1 概述
1.1 Django 目錄結(jié)構(gòu)
2 常用配置:settings.py
2.1 注冊 APP:INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles', # 以上為默認(rèn) APP
'app01.apps.App01Config', # 以下是新增的應(yīng)用
]
對應(yīng)項(xiàng)目下的 apps.py 文件,如:
2.2 模板路徑:TEMPLATES
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
],
},
},
]
- 在根目錄下添加 templates 文件夾,可實(shí)現(xiàn)跳轉(zhuǎn)至對應(yīng)名稱的 HTML 頁面
2.3 靜態(tài)文件:STATICFILES_DIRS
# 靜態(tài)文件:CSS、JS、IMG 等
# 靜態(tài)文件的 url
STATIC_URL = '/static/'
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# 方式1:項(xiàng)目內(nèi)創(chuàng)建靜態(tài)文件(默認(rèn))
# 方式2:項(xiàng)目外創(chuàng)建靜態(tài)文件
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'app01/static'), # 項(xiàng)目內(nèi)的靜態(tài)文件(默認(rèn))
os.path.join(BASE_DIR, 'static'), # 項(xiàng)目外的靜態(tài)文件
)
方式1:項(xiàng)目內(nèi)創(chuàng)建 static 文件夾文章來源:http://www.zghlxwxcb.cn/news/detail-718791.html
方式2:項(xiàng)目外創(chuàng)建 static 文件夾文章來源地址http://www.zghlxwxcb.cn/news/detail-718791.html
2.4 數(shù)據(jù)庫:DATABASES
# 默認(rèn)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# 修改為其他數(shù)據(jù)庫,如:mysql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test', # 數(shù)據(jù)庫名稱
'USER': 'root', # 用戶名
'PASSWORD': '123456', # 密碼
'HOST': '127.0.0.1', # 主機(jī)
'PORT': '3306', # 端口號
}}
2.5 允許訪問的主機(jī):ALLOWED_HOSTS
ALLOWED_HOSTS = [] # 默認(rèn) 127.0.0.1 或 localhost
ALLOWED_HOSTS = [*] # 所有
ALLOWED_HOSTS = ['192.168.100.1', '127.0.0.1']
到了這里,關(guān)于Python Django 之全局配置 settings 詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!