django settings.py配置

史洛城
2023-12-01

1.配置静态文件

#STATIC_URL = '/static/'为静态文件别名
STATIC_URL = '/static/'
#静态文件地址拼接,后面'static'文件为自己建立的存放静态文件(JS,IMG,CSS)的文件名
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'), #主文件下静态文件
    os.path.join(BASE_DIR,"blog","statics"),#项目blog文件下静态文件
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.mysql数据库配置

########在setting原来默认的sqlite
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
############修改成mysql如下
DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.mysql',

        'NAME': 'test',    #你的数据库名称

        'USER': 'root',   #你的数据库用户名

        'PASSWORD': '19941028', #你的数据库密码

        'HOST': '', #你的数据库主机,留空默认为localhost

        'PORT': '3306', #你的数据库端口
    }}
#由于mysql默认引擎为MySQLdb,在__init__.py文件中添加下面代码
#在python3中须替换为pymysql,可在主配置文件(和项目同名的文件下,不是app配置文件)中增加如下代码
import pymysql
pymysql.install_as_MySQLdb()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

3.设置打印日志到屏幕

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['console'],
            'propagate': True,
            'level':'DEBUG',
        },
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

4.语言

LANGUAGE_CODE = 'en-us'# 默认
LANGUAGE_CODE = 'zh-hans'# 改为中文,主要针对admin页面
  • 1
  • 2

5.配置模板

TEMPLATE_DIRS = (
        os.path.join(BASE_DIR,'templates'),
    )
  • 1
  • 2
  • 3

6.setting文件默认代码注释

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__)))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '@jm-+mpxpv-@3%=o^fij9w+dtsil=18&6bpt*akkv+=422-vsl'

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

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 = 'django_lesson.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',
            ],
        },
    },
]

#网页服务网关协议消息
WSGI_APPLICATION = 'django_lesson.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
#数据库配置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.10/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/1.10/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/1.10/howto/static-files/

# 静态文件
STATIC_URL = '/static/'
#单级目录静态文件
# STATICFILES_DIRS=(
#     os.path.join(BASE_DIR,"statics"),
# )
#多级目录静态文件
STATICFILES_DIRS=(
    os.path.join(BASE_DIR,"blog","statics"),
)
 类似资料: