个人博客开发之 全局配置文件settings设置
Posted 池偏一
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了个人博客开发之 全局配置文件settings设置相关的知识,希望对你有一定的参考价值。
项目源码下载:http://download.vhosts.cn
# -*- coding: utf-8 -*- """ Django settings for cpyblog project. Generated by ‘django-admin startproject‘ using Django 1.11.18. For more information on this file, see https://docs.djangoproject.com/en/1.11/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.11/ref/settings/ """ import os import sys # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 将apps 和extra_apps 两个包加入到根搜索路径,方便使用时可以直接导入模块,而不用从根目录开始一级一级导入 sys.path.insert(0, os.path.join(BASE_DIR, ‘apps‘)) sys.path.insert(0, os.path.join(BASE_DIR, ‘extra_apps‘)) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = ‘v+=x5qo3dkg_)nfag_#avt8u)&s$1nz5f#*f_ut4k72-6vh#ig‘ # SECURITY WARNING: don‘t run with debug turned on in production! # 开启debug 调试模式 DEBUG = True # 添加允许访问的主机列表,可以是ip也可以是域名,*代表所有 ALLOWED_HOSTS = ["*"] # Application definition # 将app 加入到列表中 INSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘xadmin‘, # 添加xadmin 第三方app ‘DjangoUeditor‘, # 添加djangoueditor 第三方app ‘users‘, # 添加users app ‘blogs‘, # 添加blogs app ‘crispy_forms‘, # 添加xadmin中表单美化 app ] # 修改默认的用户认证为UserProfile表 AUTH_USER_MODEL = ‘users.UserProfile‘ # django2.X 以上MIDDLEWARE_CLASSES 改成MIDDLEWARE 如果版本不匹配可能出现:‘WSGIRequest‘ object has no attribute ‘user‘ MIDDLEWARE = [ ‘django.middleware.security.SecurityMiddleware‘, ‘django.contrib.sessions.middleware.SessionMiddleware‘, ‘django.middleware.common.CommonMiddleware‘, ‘django.middleware.csrf.CsrfViewMiddleware‘, ‘django.contrib.auth.middleware.AuthenticationMiddleware‘, ‘django.contrib.messages.middleware.MessageMiddleware‘, ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘, ] # 默认根路由文件 ROOT_URLCONF = ‘cpyblog.urls‘ # 默认html 模板文件位置 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‘, ], }, }, ] WSGI_APPLICATION = ‘cpyblog.wsgi.application‘ # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases # 数据库配置 DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘cpyblog‘, # 数据库名 ‘HOST‘: ‘localhost‘, # 数据库地址 ‘PORT‘: 3306, # 数据库端口 ‘USER‘: ‘root‘, # 登录名 ‘PASSWORD‘: ‘xxxxxxxxxx‘ # 登录密码 } } # Password validation # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { ‘NAME‘: ‘django.contrib.auth.password_validation.UserAttributeSimilarityValidator‘, }, { ‘NAME‘: ‘django.contrib.auth.password_validation.MinimumLengthValidator‘, }, { ‘NAME‘: ‘django.contrib.auth.password_validation.CommonPasswordValidator‘, }, { ‘NAME‘: ‘django.contrib.auth.password_validation.NumericPasswordValidator‘, }, ] # Internationalization # https://docs.djangoproject.com/en/1.11/topics/i18n/ LANGUAGE_CODE = ‘zh-Hans‘ # 使用中文简体 TIME_ZONE = ‘Asia/Shanghai‘ # 时区使用亚洲上海 USE_I18N = True USE_L10N = True USE_TZ = False # Static files (CSS, javascript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ # 将static加入到staticfile 查找目录中 STATICFILES_DIRS = [ os.path.join(BASE_DIR, ‘static‘), ] # 定义static目录的访问url STATIC_URL = ‘/static/‘ STATIC_ROOT = os.path.join(BASE_DIR, ‘all_static‘) # 定义media 根目录 MEDIA_ROOT = os.path.join(BASE_DIR, ‘media‘) MEDIA_URL = ‘/media/‘ # session 配置 SESSION_ENGINE = ‘django.contrib.sessions.backends.db‘ SESSION_COOKIE_NAME = ‘sessionid‘ SESSION_COOKIE_PATH = ‘/‘ # session的cookie 保存的路径(默认为根) SESSION_COOKIE_DOMAIN = None # session的cookie保存的域名(默认为None) SESSION_COOKIE_HTTPONLY = True # session的cookie传输只支持http传输 # 每次request请求都重新刷新session缓存 SESSION_SAVE_EVERY_REQUEST = True # 关闭浏览器session失效 SESSION_EXPIRE_AT_BROWSER_CLOSE = True # session过期时间为5分钟,因为前面设置了每次请求都刷新session # 所以可以看作5分钟不操作则过期 SESSION_COOKIE_AGE = 60 * 5 # 配置日志文件目录和日志存放格式等 LOG_DIR = os.path.join(BASE_DIR, ‘logs‘) LOGGING = { ‘version‘: 1, ‘disable_existing_loggers‘: True, ‘formatters‘: { ‘logout‘: { ‘format‘: ‘[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s‘, ‘datefmt‘: ‘%Y-%m-%d %H:%M:%S‘ }, }, ‘filters‘: { ‘require_debug_false‘: { ‘()‘: ‘django.utils.log.RequireDebugFalse‘, }, ‘require_debug_true‘: { ‘()‘: ‘django.utils.log.RequireDebugTrue‘, }, }, ‘handlers‘: { ‘business‘: { ‘level‘: ‘INFO‘, ‘filters‘: [‘require_debug_false‘], ‘class‘: ‘logging.handlers.RotatingFileHandler‘, ‘filename‘: os.path.join(LOG_DIR, ‘blog.log‘), ‘maxBytes‘: 1024 * 1024 * 5, ‘backupCount‘: 1, ‘formatter‘: ‘logout‘, }, ‘console‘: { ‘level‘: ‘DEBUG‘, ‘filters‘: [‘require_debug_true‘], ‘class‘: ‘logging.StreamHandler‘, ‘formatter‘: ‘logout‘, }, ‘author‘: { ‘level‘: ‘INFO‘, ‘filters‘: [‘require_debug_false‘], ‘class‘: ‘logging.handlers.RotatingFileHandler‘, ‘filename‘: os.path.join(LOG_DIR, ‘author.log‘), ‘maxBytes‘: 1024 * 1024 * 5, ‘backupCount‘: 1, ‘formatter‘: ‘logout‘, } }, ‘loggers‘: { ‘django.request‘: { ‘handlers‘: [‘console‘], ‘level‘: ‘DEBUG‘, ‘propagate‘: False, }, ‘blog‘: { ‘handlers‘: [‘business‘], ‘level‘: ‘INFO‘, ‘propagate‘: True, }, ‘author‘: { ‘handlers‘: [‘author‘], ‘level‘: ‘INFO‘, ‘propagate‘: True, } } }
以上是关于个人博客开发之 全局配置文件settings设置的主要内容,如果未能解决你的问题,请参考以下文章