1. 程式人生 > >Django配置-settings.py

Django配置-settings.py

cached signed rect sessionid sql redirect -h 返回 stat

import os

#向上找兩級,獲取基地址
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = ‘tt^-bhdmqd-^pet%puk*q58gcqg-g)9h+97!4m9aveddp_$%5e‘

DEBUG = True

ALLOWED_HOSTS = []

#APP信息,自己新建的項目如果沒有自動加入,可以再次手動加進去
INSTALLED_APPS = [
    ‘django.contrib.admin‘,
    ‘django.contrib.auth‘,
    ‘django.contrib.contenttypes‘,
    ‘django.contrib.sessions‘,
    ‘django.contrib.messages‘,
    ‘django.contrib.staticfiles‘,
    ‘app01‘,
    ‘backend‘,
    ‘web‘,
]

#中間件配置
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‘,
    # "app01.my_middlewares.AuthMiddleware",  #註意自己寫的中間件,配置要寫在系統中的後面
]

ROOT_URLCONF = ‘myblog.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‘,
            ],
        },
    },
]

# #配置模板
# TEMPLATE_DIRS = (
#         os.path.join(BASE_DIR,‘templates‘),
# )

#網頁服務網關協議消息
WSGI_APPLICATION = ‘myblog.wsgi.application‘


#原來默認的sqlite數據庫
# DATABASES = {
#     ‘default‘: {
#         ‘ENGINE‘: ‘django.db.backends.sqlite3‘,
#         ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘),
#     }
# }

#mysql數據庫 同時需要設置__init__.py
# import pymysql
# pymysql.install_as_MySQLdb()

DATABASES = {
                ‘default‘: {
                    ‘ENGINE‘: ‘django.db.backends.mysql‘,
                    ‘NAME‘:‘myblog‘, #數據庫名
                    ‘USER‘: ‘root‘,
                    ‘PASSWORD‘: ‘‘,
                    ‘HOST‘: ‘localhost‘,
                    ‘PORT‘: 3306,
                    }
				}

#認證相關
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‘,
    },
]

# 語言
LANGUAGE_CODE = ‘zh-hans‘  # 主要針對admin頁面
# 時間
TIME_ZONE = ‘Asia/Shanghai‘

USE_I18N = True
USE_L10N = True

#時區設置
USE_TZ = False

#靜態文件
STATIC_URL = ‘/static/‘  # 靜態文件別名
STATICFILES_DIRS=(
    os.path.join(BASE_DIR,‘static‘),  #主文件下靜態文件
)

# 與用戶上傳相關的配置
#路由urls.py中也需要配置
"""
# media配置:
from django.urls import path,re_path
from django.views.static import serve
from django.conf import settings

urlpatterns = [
    re_path(r"media/(?P<path>.*)$",serve,{"document_root":settings.MEDIA_ROOT}),
]
"""

MEDIA_ROOT=os.path.join(BASE_DIR,"media")
MEDIA_URL="/media/"

# 引用Django自帶的User表,繼承使用時需要設置
#如果數據庫中的UserInfo(用戶表)繼承django內置AbstractUser
#model需導入 from django.contrib.auth.models import AbstractUser
AUTH_USER_MODEL="app01.UserInfo"  #應用名.UserInfo

# 自定義配置登陸驗證返回路徑
LOGIN_URL="/login/"
LOGIN_REDIRECT_URL = ‘/‘
LOGOUT_URL = ‘/logout/‘




#
EMAIL_HOST = ‘smtp.exmail.qq.com‘  # 如果是 163 改成 smtp.163.com
EMAIL_PORT = 465
EMAIL_HOST_USER = ‘‘           # 帳號
EMAIL_HOST_PASSWORD = ‘‘  # 密碼
# DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_USE_SSL = True

#session存儲的相關配置
# Django默認支持Session,並且默認是將Session數據存儲在數據庫中,即:django_session表中。提供了5種類型的Session
"""
#1.數據庫
SESSION_ENGINE = ‘django.contrib.sessions.backends.db‘  # 引擎(默認)
#2.緩存
SESSION_ENGINE = ‘django.contrib.sessions.backends.cache‘  # 引擎
SESSION_CACHE_ALIAS = ‘default‘
# 要配合Django緩存配置
# 使用的緩存別名(默認內存緩存,也可以是memcache),此處別名依賴緩存的設置
#3.文件
SESSION_ENGINE = ‘django.contrib.sessions.backends.file‘    # 引擎
SESSION_FILE_PATH = None
# 緩存文件路徑,如果為None,則使用tempfile模塊獲取一個臨時地址tempfile.gettempdir()
#4.緩存+數據庫
SESSION_ENGINE = ‘django.contrib.sessions.backends.cached_db‘        # 引擎
#5.加密cookie 基本沒有用
SESSION_ENGINE = ‘django.contrib.sessions.backends.signed_cookies‘   # 引擎
"""

# SESSION_COOKIE_NAME = "sessionid"                       # Session的cookie保存在瀏覽器上時的key,即:sessionid=隨機字符串(默認)
# SESSION_COOKIE_PATH = "/"                               # Session的cookie保存的路徑(默認)
# SESSION_COOKIE_DOMAIN = None                             # Session的cookie保存的域名(默認)
# SESSION_COOKIE_SECURE = False                            # 是否Https傳輸cookie(默認)
# SESSION_COOKIE_HTTPONLY = True                           # 是否Session的cookie只支持http傳輸(默認)
# SESSION_COOKIE_AGE = 1209600                             # Session的cookie失效日期(2周)(默認)
# SESSION_EXPIRE_AT_BROWSER_CLOSE = False                  # 是否關閉瀏覽器使得Session過期(默認)
# SESSION_SAVE_EVERY_REQUEST = True        # (每次刷新更新時間) 是否每次請求都保存Session,默認修改之後才保存(默認)

SESSION_COOKIE_AGE = 60 * 60 * 24
SESSION_SAVE_EVERY_REQUEST = True

#打印日誌到屏幕
LOGGING = {
    ‘version‘: 1,
    ‘disable_existing_loggers‘: False,
    ‘handlers‘: {
        ‘console‘:{
            ‘level‘:‘DEBUG‘,
            ‘class‘:‘logging.StreamHandler‘,
        },
    },
    ‘loggers‘: {
        ‘django.db.backends‘: {
            ‘handlers‘: [‘console‘],
            ‘propagate‘: True,
            ‘level‘:‘DEBUG‘,
        },
    }
}

  

Django配置-settings.py