app注册
INSTALLED_APPS = [
...
'django.contrib.staticfiles',
'app01',
]
模板文件夹templates配置
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的配置
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static'),
]
Media配置:在settings文件下添加这句代码
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
sqlite3换mysql的配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Django_db',
'USER':'root',
'PASSWORD':'root',
'HOST':'127.0.0.1',
'PORT':3306,
'CHARSET':'utf8'
}
}
Django终端打印SQL语句的配置
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console':{
'level':'DEBUG',
'class':'logging.StreamHandler',
},
},
'loggers': {
'django.db.backends': {
'handlers': ['console'],
'propagate': True,
'level':'DEBUG',
},
}
}
扩展默认auth_user表的字段配置。
AUTH_USER_MODEL='应用名.表名'
rest_framework注册
INSTALLED_APPS = [
...
'app01.apps.App01Config',
'rest_framework',
]
Rest_framework 响应全局配置
-全局使用:全局的视图类,所有请求,都有效
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
)
}
drf认证全局配置:
REST_FRAMEWORK={
"DEFAULT_AUTHENTICATION_CLASSES":["app01.app_auth.MyAuthentication",]
}
drf相关的全局配置:
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": ["app01.app_auth.Login_auth", ],
"DEFAULT_PERMISSION_CLASSES": ['app01.app_per.User_per', ],
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle',
),
'DEFAULT_THROTTLE_RATES': {
'anon': '3/m',
'user': '5/m'
},
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
'EXCEPTION_HANDLER': 'app01.wsigi.APIhandle_exceptn'
}
分页器全局配置:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 100
}
Django设置中文语言
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
jwt配置
JWT_AUTH={
'JWT_RESPONSE_PAYLOAD_HANDLER':'app01.utils.new_jwt_response_payload_handler',
'JWT_EXPIRATION_DELTA': datetime.timedelta(days=7),
}