1.调试
DEBUG = True
2.设置template
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', ], }, }, ]
3.设置mysql数据
DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.mysql', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'NAME': 'ttsx_bj5', 'USER': 'root', 'PASSWORD': 'mysql', 'HOST': 'localhost', 'PORT': 3306, } }
4.设置后台中文格式
# LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'zh-Hans' # TIME_ZONE = 'UTC' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = True
5.设置静态文件
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] # 设置静态文件
6.设置图片上传路径
MEDIA_ROOT = os.path.join(BASE_DIR, "static/goods") # 配置图片上传路径
7.添加搜索路径
sys.path.insert(0, BASE_DIR) sys.path.insert(0, os.path.join(BASE_DIR, 'apps')) sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
8.配置日志
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': ('%(levelname)s [%(asctime)s] [%(name)s:%(module)s:%(funcName)s:%(lineno)s] ' '[%(exc_info)s] %(message)s'), 'datefmt': "%d/%b/%Y %H:%M:%S", }, }, 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse', }, 'require_debug_true': { '()': 'django.utils.log.RequireDebugTrue', }, }, 'handlers': { 'console': { 'level': 'DEBUG', 'filters': ['require_debug_true'], 'class': 'logging.StreamHandler', 'formatter': 'standard', }, 'file': { 'level': 'INFO', 'class': 'logging.FileHandler', 'filename': os.path.join(BASE_DIR + '/logs/', 'access.log'), 'formatter': 'standard', }, 'celery': { 'level': 'INFO', 'class': 'logging.FileHandler', 'filename': os.path.join(BASE_DIR + '/logs/', 'celery.log'), 'formatter': 'standard', }, 'rpc': { 'level': 'INFO', 'class': 'logging.FileHandler', 'filename': os.path.join(BASE_DIR + '/logs/', 'rpcserver.log'), 'formatter': 'standard', }, }, 'loggers': { 'django': { 'handlers': ['console', 'file'], 'level': 'DEBUG', 'propagate': False, }, 'django.request': { 'handlers': ['console', 'file'], 'level': 'INFO', 'propagate': False, }, 'apis': { 'handlers': ['console', 'file'], 'level': 'DEBUG', 'propagate': False, }, } }
9.rest framework配置
setting.py INSTALLED_APPS = ( ... 'rest_framework', ) REST_FRAMEWORK = { # Use Django's standard `django.contrib.auth` permissions, # or allow read-only access for unauthenticated users. 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' ] } urls.py urlpatterns = [ ... url(r'^api-auth/', include('rest_framework.urls')) ]
aaa