Python-Web-Django: settings.py Configuration
Install REST Framework pip install djangorestframework pip install django-rest-swagger After installing REST Framework, the previously created Django App(hello_app) can communicate using the REST API. All Apps we create must be added to the INSTALLED_APPS field. INSTALLED_APPS = [ #
目录8 个章节
Install REST Framework
pip install djangorestframework
pip install django-rest-swagger
After installing REST Framework, the previously created Django App (hello_app) can communicate using the REST API.
All Apps we create must be added to the INSTALLED_APPS field.
INSTALLED_APPS = [
# REST Framework
'rest_framework',
'rest_framework_swagger',
# Custom App
'hello_app'
]
Database Configuration
Django supports multiple backend databases (MySQL, Oracle, PostgreSQL, etc.). I'll still use the default SQLite first, as it's more convenient.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Web Page Templates
To render web pages in Django, you first need to provide templates. Set the HTML template folder in DIRS; here we conventionally use the templates folder.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Static Resources
MEDIA_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
Site URL Settings
Used for the server's URL routing configuration.
In the code below, hello_django.urls refers to the urls.py file of the hello_django project.
ROOT_URLCONF = 'hello_django.urls'
Logging Configuration
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '%(asctime)s %(levelname)s %(name)s %(message)s',
},
},
'handlers': {
'default': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'hello_django.log',
'maxBytes': 1024*1024*10, # 10 MB
'backupCount': 5,
'formatter': 'standard'
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': True
}
}
}
After configuring log output, all output using the logging library will be recorded in the hello_django.log file.
At this point, the configuration items in settings.py are basically all set up.
About
For more interesting operations, please follow my WeChat official account: DealiAxy Every article is also collected on my blog: blog.deali.cn
_EOF_
Python-Web-Django: settings.py Configuration
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Python-Web-Django:第一个Web-App的构建
View视图渲染 编辑App目录下的views.py from django.shortcuts import render from django.http import HttpResponse # Create your views here. def hello(request): return HttpResponse(Hello Django!) URL解析配置 编辑urls
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
Python-Web:Django模板继承
模板可以用继承的方式来实现复用。 接下来我们先创建之前项目的 templates 目录中添加 base.html 文件,代码如下: !DOCTYPE html html head meta charset=utf-8 title标题/title /head body h1Hello World!/h1 pDA Django 测试。/p {% block mainbody %
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
Python-Web:Django-模板标签
if/else 标签 基本语法格式如下: {% if condition %} ... display {% endif %} 或者: {% if condition1 %} ... display 1 {% elif condition2 %} ... display 2 {% else %} ... display 3 {% endif %} 根据条件判断是否输出。
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
Python-Web-Django:开始Django之旅
前言 开源的Web框架Django简单易用,稳定性和灵活性高,因此被广泛应用于商业化环境,它充分利用了Python拥有丰富的库这一优势。 Django的安装和服务器的搭建 安装Django库: pip install django 新建Web应用: django-admin startproject hello_django 以上命令会在当前文件夹下建立hello_django目录。 目录结构如下
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
讨论
共 0 条加载评论中…
