django-mongoengine基本配置

it2026-06-03  4

""" Django settings for beecloud_api project. Generated by 'django-admin startproject' using Django 3.0.4. For more information on this file, see https://docs.djangoproject.com/en/3.0/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.0/ref/settings/ """ import datetime 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__))) # 添加导包路径 import sys sys.path.insert(0, os.path.join(BASE_DIR, 'apps')) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ['127.0.0.1', '0.0.0.0'] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django_mongoengine', 'django_mongoengine.mongo_auth', 'django_mongoengine.mongo_admin', 'corsheaders', 'rest_framework', 'rest_framework_mongoengine', 'rest_framework_jwt', 'user.apps.UserConfig', 'productions.apps.ProductionsConfig', 'gateway.apps.GatewayConfig', ] MIDDLEWARE = [ '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 = 'beecloud_api.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 = 'beecloud_api.wsgi.application' # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases MONGODB_DATABASES = { "default": { "name": "数据库名", "db": "数据库名", "username": "MongoDB登录用户名", "password": "MongoDB登录密码", "host": "数据库IP", "port": 端口号, # "authentication_source": True, "authentication_mechanism": "加密方式SCRAM-SHA-1", "tz_aware": True, # if you use timezones in django (USE_TZ = True) } } # Password validation # https://docs.djangoproject.com/en/3.0/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/3.0/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/3.0/howto/static-files/ STATIC_URL = '/static/' STATIC_ROOT = os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), "frontend/static") CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://:assurelink@192.168.1.104:6379/0", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } }, "session": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://:assurelink@192.168.1.104:6379/1", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } } } # SESSION_ENGINE = "django.contrib.sessions.backends.cache" SESSION_ENGINE = "django_mongoengine.sessions" SESSION_CACHE_ALIAS = "session" SESSION_SERIALIZER = 'django_mongoengine.sessions.BSONSerializer' 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在debug模式下才输出日志 '()': '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/beecloud_api.log"), # 日志文件的位置 'maxBytes': 300 * 1024 * 1024, 'backupCount': 10, 'formatter': 'verbose' }, }, 'loggers': { # 日志器 'django': { # 定义了一个名为django的日志器 'handlers': ['console', 'file'], # 可以同时向终端与文件中输出日志 'propagate': True, # 是否继续传递日志信息 'level': 'INFO', # 日志器接收的最低日志级别 }, } } REST_FRAMEWORK = { # 异常处理 'EXCEPTION_HANDLER': 'beecloud_api.utils.exceptions.exception_handler', 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema', 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', ), } CORS_ORIGIN_WHITELIST = ( 'http://127.0.0.1:9527', 'http://192.168.1.100:9527', 'http://127.0.0.1:8000', 'http://192.168.1.104:8000' ) CORS_ALLOW_CREDENTIALS = True # 允许携带cookie # AUTH_USER_MODEL = 'user.apps.UserConfig' # AUTH_USER_MODEL = 'user.User' # AUTH_USER_MODEL = 'productions.Production' # AUTH_USER_MODEL = 'user.CarePeople' AUTH_USER_MODEL = 'mongo_auth.MongoUser' # 指定auth_user_model MONGOENGINE_USER_DOCUMENT = 'user.models.User' # 记得加指定的Document JWT_AUTH = { 'JWT_EXPIRATION_DELTA': datetime.timedelta(days=1), # jwt 有效期配置 'JWT_RESPONSE_PAYLOAD_HANDLER': 'user.utils.jwt_response_payload_handler', # 返回结果配置 } AUTHENTICATION_BACKENDS = [ 'django_mongoengine.mongo_auth.backends.MongoEngineBackend', # 'user.utils.UsernameMobileAuthBackend', # 'mongoengine.django.auth.MongoEngineBackend', ]
最新回复(0)