django项目配置
目录结构: . settings/dev.py是配置文件 配置文件代码 """ Django settings for luffyapi project. Generated by ‘django-admin startproject‘ using Django 2.2.5. For more information on this file,see https://docs.djangoproject.com/en/2.2/topics/settings/ For the full list of settings and their values,see https://docs.djangoproject.com/en/2.2/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文件夹添加到系统识别的路径中 sys.path.insert(0,os.path.join(BASE_DIR,"apps")) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = ‘6pmbe)o5)1&^66z2_21itq)l_s5+ch^=gm6psqf&[email?protected]^@y‘ # SECURITY WARNING: don‘t run with debug turned on in production! DEBUG = True # 访问api的url ALLOWED_HOSTS = [ "api.luffycity.cn",] # Application definition INSTALLED_APPS = [ ‘django.contrib.admin‘,‘django.contrib.auth‘,‘django.contrib.contenttypes‘,‘django.contrib.sessions‘,‘django.contrib.messages‘,‘django.contrib.staticfiles‘,# 第三方app ‘corsheaders‘,‘rest_framework‘,‘xadmin‘,‘crispy_forms‘,‘reversion‘,# 自定义app ‘home‘,‘users‘,‘course‘,] # CORS组的配置信息,允许跨域访问白名单 CORS_ORIGIN_WHITELIST = ( ‘http://www.luffycity.cn:8080‘,) CORS_ALLOW_CREDENTIALS = False # 允许ajax跨域请求时携带cookie MIDDLEWARE = [ # 解决ajax跨域访问中间件 ‘corsheaders.middleware.CorsMiddleware‘,‘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 = ‘luffyapi.urls‘ TEMPLATES = [ { ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘,‘DIRS‘: [],‘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 = ‘luffyapi.wsgi.application‘ # Database # https://docs.djangoproject.com/en/2.2/ref/settings/#databases # MySQL数据库连接配置 DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘,‘NAME‘: "luffy",‘HOST‘: ‘127.0.0.1‘,‘PORT‘: 3306,‘USER‘: ‘luffy_user‘,‘PASSWORD‘: ‘luffy‘,} } # 配置redis数据库链接 CACHES = { # 默认缓存 "default": { # 数据缓存数据的类名 "BACKEND": "django_redis.cache.RedisCache",# 项目上线时,需要调整这里的路径 "LOCATION": "redis://127.0.0.1:6379/0",# 其他配置项 "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient",} },# 提供给xadmin或者admin的session存储 "session": { # 数据缓存数据的类名 "BACKEND": "django_redis.cache.RedisCache",需要调整这里的路径 "LOCATION": "redis://127.0.0.1:6379/1",# 提供存储短信验证码 "sms_code": { # 数据缓存数据的类名 "BACKEND": "django_redis.cache.RedisCache",} # 设置xadmin用户登录时,登录信息session保存到redis SESSION_ENGINE = "django.contrib.sessions.backends.cache" SESSION_CACHE_ALIAS = "session" # Password validation # https://docs.djangoproject.com/en/2.2/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/2.2/topics/i18n/ LANGUAGE_CODE = ‘zh-Hans‘ TIME_ZONE = ‘Asia/Shanghai‘ USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS,JavaScript,Images) # https://docs.djangoproject.com/en/2.2/howto/static-files/ #访问静态文件的url前缀 STATIC_URL = ‘/static/‘ #设置django的静态文件目录 STATICFILES_DIRS=[ os.path.join(BASE_DIR,"static") ] #上传文件目录 MEDIA_ROOT = os.path.join(BASE_DIR,"uploads") # 上传文件url地址前缀 MEDIA_URL = ‘/media/‘ # 日志文件配置 LOGGING = { ‘version‘: 1,‘disable_existing_loggers‘: False,‘formatters‘: { ‘verbose‘: { ‘format‘: ‘%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s‘ },‘simple‘: { ‘format‘: ‘%(levelname)s %(module)s %(lineno)d %(message)s‘,‘filters‘: { ‘require_debug_true‘: { ‘()‘: ‘django.utils.log.RequireDebugTrue‘,‘handlers‘: { ‘console‘: { ‘level‘: ‘INFO‘,‘filters‘: [‘require_debug_true‘],‘class‘: ‘logging.StreamHandler‘,‘formatter‘: ‘simple‘ },‘file‘: { ‘level‘: ‘INFO‘,‘class‘: ‘logging.handlers.RotatingFileHandler‘,# 日志位置,日志文件名,日志保存目录必须手动创建 ‘filename‘: os.path.join(os.path.dirname(BASE_DIR),"logs/luffy.log"),# 日志文件的最大值,这里我们设置300M ‘maxBytes‘: 300 * 1024 * 1024,# 日志文件的数量,设置最大日志数量为10 ‘backupCount‘: 10,# 日志格式:详细格式 ‘formatter‘: ‘verbose‘ },‘loggers‘: { ‘django‘: { ‘handlers‘: [‘console‘,‘file‘],‘propagate‘: True,} } REST_FRAMEWORK ={ # 修改错误类判断方法,可以识别数据库错误 ‘EXCEPTION_HANDLER‘:‘luffyapi.utils.exceptions.custom_exception_handler‘,# jwt验证配置 ‘DEFAULT_AUTHENTICATION_CLASSES‘: ( ‘rest_framework_jwt.authentication.JSONWebTokenAuthentication‘,‘rest_framework.authentication.SessionAuthentication‘,‘rest_framework.authentication.BasicAuthentication‘,),} # 使用自定义用户模型类 AUTH_USER_MODEL = ‘users.User‘ import datetime JWT_AUTH = { # jwt验证保存时间 "JWT_EXPIRATION_DELTA":datetime.timedelta(days=1),# 自定义jwt验证返回之 ‘JWT_RESPONSE_PAYLOAD_HANDLER‘: ‘users.utils.jwt_response_payload_handler‘,} # 自定义登录方式 AUTHENTICATION_BACKENDS = [ "users.utils.UsernameMobileAuthBackend" ] # 短信接口配置,可登陆官网查看 SMS = { # 开发者账号 "_accountSid":"8aaf07086d05d00c016d1f327e740e2a",# token验证码 "_accountToken":"2196b6f36f6d4bdc8b5e6bd791c57dbd",# appid "_appId":"8aaf07086d05d00c016d1f327ed10e31",# id地址 "_serverIP":"sandboxapp.cloopen.com",# 端口号 "_serverPort":"8883",# API版本号 "_softVersion":"2013-12-26",} # 邮箱配置 EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend‘ #email后端 EMAIL_USE_TLS = False #是否使用TLS安全传输协议 EMAIL_USE_SSL = True #是否使用SSL加密,qq企业邮箱要求使用 EMAIL_HOST = ‘smtp.163.com‘ #发送邮件的邮箱 的 SMTP服务器,这里用了qq企业邮箱 EMAIL_PORT = 465 #发件箱的SMTP服务器端口 EMAIL_HOST_USER = ‘[email?protected]‘ #发送邮件的邮箱地址 EMAIL_HOST_PASSWORD = ‘ads2504141441sbs‘ #发送邮件的邮箱密码 DEFAULT_FROM_EMAIL = EMAIL_HOST_USER (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |