【django学习-03】Django的配置文件(settings)

  • Django的配置文件setting.py用于配置整个网站的环境和功能,核心配置必须有项目路径、秘钥配置、域名访问权限、APP列表、中间件、资源文件、模板配置、数据库的链接方式等。
"""
Django settings for mysite0828 project.

Generated by 'django-admin startproject' using Django 3.2.13.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
# BASE_DIR的作用就是提供给项目中所有需要使用路径的内容提供一个公共的不依赖于操作系统的基本路径起点。
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
#秘钥配置SECRET_KEY,这是一个随机值,创建项目的时候自动生成,主要用于重要数据的加密处理,如:用户密码、CSRF机制和#Session等
SECRET_KEY = 'django-insecure-_fbshcls)c!2z!kv%$)&uzh)zkfpj()lg18r_cof6g#vrp7s-l'

# SECURITY WARNING: don't run with debug turned on in production!
#调试模式
DEBUG = True

#域名访问权限
ALLOWED_HOSTS = []
#当DEBUG = True,ALLOWED_HOSTS为空列表时,只运行localhost在浏览器上访问
#当DEBUG = False时,ALLOWED_HOSTS必填,不然项目无法启动,如果想设置所有域名允许访问,可设置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',
    'blog',
]

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 = 'mysite0828.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 = 'mysite0828.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.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/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/allstatics/'

STATICFILES_DIRS = (
    # BASE_DIR / 'statics',
    os.path.join(BASE_DIR,"statics"),
)

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
  • STATIC_URL和STATICFILES_DIRS
    • STATIC_URL = '/static/' #引用名
    • STATICFILES_DIRS = (
      os.path.join(BASE_DIR,"statics") #实际名 ,即实际文件夹的名字
      )
posted @ 2022-08-30 20:27  Tony_xiao  阅读(124)  评论(0编辑  收藏  举报