2、python web方向Django新手入門—settings篇

What about settings?

這裏寫圖片描述

【settings.py】

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#配置根目錄

SECRET_KEY = 'p63#%0q4yw2ttbsmbw667xo*cqz_shj#^fpk8bgowomjmg=#%%'
#密鑰

DEBUG = True
#開啓調試模式
#1、視圖函數變動,自動重啓服務器
#2、錯誤詳細輸出,環境變量
#3、生產環境需要關閉


ALLOWED_HOSTS = []
#允許訪問的主機地址,*表示所有都可以訪問


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'cmdb',
]
#APP儲存新建APP需要手動添加進來



MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    #csrf防跨站保護機制
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
#中間件,響應之前進行的自動處理


ROOT_URLCONF = 'play.urls'
#項目的配置文件路徑,如果改文件包,這個也需要改。


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 = 'play.wsgi.application'
#WSGI路徑

#數據庫連接配置
DATABASES = {
    # 'default': {
    #     'ENGINE': 'django.db.backends.sqlite3',
    #     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    #}
    # db.sqlite3是django自帶的小型數據庫ke'yi
    'default':{
        'ENGINE':'django.db.backends.mysql',
        'NAME':'test',
        'HOST':'127.0.0.1',
        'USER':'root',
        'PASSWORD':'111111',
        'POST':3306,
    }#配置連接本機mysql
    }

#配置後臺管理的語言
LANGUAGE_CODE = 'zh-hans'#中文
#配置後臺時間
TIME_ZONE = 'Asia/Shanghai'#亞洲上海時間,只有上海時間和重慶時間


#設置靜態文件的路由(url)地址
STATIC_URL = '/static/'
#靜態文件地址
STATICFILES_UIRS ={
    os.path.join(BASE_DIR,'static')
}

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
#     If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the default) then
# all loggers from the default configuration will be disabled. Disabled loggers are not the same as removed; the logger
# will still exist, but will silently discard anything logged to it, not even propagating entries to a parent logger. Thus
# you should be very careful using ’disable_existing_loggers’: True; it’s probably not what you want.
# Instead, you can set disable_existing_loggers to False and redefine some or all of the default loggers; or
# you can set LOGGING_CONFIG to None and handle logging config yourself.
    'formatters': { #  日誌格式
        'standard': {
            'format': '%(asctime)s [%(name)s:%(lineno)d] [%(module)s:%(funcName)s] [%(levelname)s]- %(message)s'},
    },
    # 日誌處理器
    'handlers': {
        # 基於文件的處理器
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'log/debug.log',
            'formatter': 'standard'
        },
        #基於控制檯日誌
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    # 日誌器
    'loggers': {
        # 日誌器名稱:日誌器字典
        'app01': {
            'handlers': ['file','console'], # 可以設置多個handlers
            'level': 'DEBUG',
            'propagate': False,
        },
        'stu.views': {
            'handlers': ['console'],
            'level': 'ERROR',
            # propagate 如果設置爲 True 默認會向上一個級別找相應logger來繼續處理,例如這裏會找一個stu的logger來處理
            # (一般我們設置爲False不向上找)
            'propagate': False,
        },
    },
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章