Python Django | 教程 | 5.项目配置 – 文件:settings.py
在往期的日志中,我们只是简单的在一个Django的项目中创建了一个应用;
并没有对该应用做任何编程,也没有在项目中,对新创建的这个应用做任何配置;
在继续讲解应用的具体开发之前,我们需要先了解关于Django项目的配置方面的知识,以便于后面讲解应用开发的时候,能够更好的理解相关的知识点。
在一个Django的项目中,负责项目配置的文件是:settings.py。
他位于项目根目录的项目管理目录中,也就是在项目的根目录下,与当前项目同名的那个子目录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[root@oracle1 django_datacenter]# pwd /django_data/django_datacenter [root@oracle1 django_datacenter]# [root@oracle1 django_datacenter]# ls -ltr total 4 -rwxr-xr-x 1 root root 673 Jun 15 15:38 manage.py drwxr-xr-x 3 root root 108 Jun 15 15:50 django_datacenter -rw-r--r-- 1 root root 0 Jun 15 16:06 db.sqlite3 drwxr-xr-x 3 root root 123 Jun 16 08:31 blog [root@oracle1 django_datacenter]# [root@oracle1 django_datacenter]# ls -ltr django_datacenter/ total 16 -rw-r--r-- 1 root root 0 Jun 15 15:38 __init__.py -rw-r--r-- 1 root root 411 Jun 15 15:38 asgi.py -rw-r--r-- 1 root root 411 Jun 15 15:38 wsgi.py -rw-r--r-- 1 root root 759 Jun 15 15:38 urls.py -rw-r--r-- 1 root root 3272 Jun 15 15:38 settings.py drwxr-xr-x 2 root root 122 Jun 15 16:06 __pycache__ [root@oracle1 django_datacenter]# [root@oracle1 django_datacenter]# ls -ltr django_datacenter/ | grep settings.py -rw-r--r-- 1 root root 3272 Jun 15 15:38 settings.py [root@oracle1 django_datacenter]# |
该文件的内容如下:
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
[root@oracle1 django_datacenter]# cat django_datacenter/settings.py """ Django settings for django_datacenter project. Generated by 'django-admin startproject' using Django 3.2.4. 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/ """ # 导入Python模块包 from pathlib import Path # 声明父目录 # Build paths inside the project like this: BASE_DIR / 'subdir'. 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 = 'django-insecure-8yq#i@@n#=q9gn876(#)i3rvgsb&5=nd2so6lm$5$41m!+&*&m' # 切换开发模式与生产模式 # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True # 需要将主域名或IP填写到这里,才能够通过其他机器访问Django项目 ALLOWED_HOSTS = [] # Application definition # 在Django项目中,当前启用了哪些应用 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] # 在Django项目中,当前启用了那些中间件 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_datacenter.urls' # Django的模板文件的配置 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 = 'django_datacenter.wsgi.application' # Django的后端数据库的配置 # 常见的数据库都是支持的:Oracle / MySQL / PostgreSQL / ...等等 # 默认使用:SQLite数据库 # Database # https://docs.djangoproject.com/en/3.2/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } # Django的密码策略的配置 # 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/ # Django的字符集:默认英文 # 中文:zh_hans LANGUAGE_CODE = 'en-us' # Django的时区 # 东八区:Asia/Shanghai TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Django的静态资源文件 # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.2/howto/static-files/ STATIC_URL = '/static/' # Default primary key field type # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' [root@oracle1 django_datacenter]# |
在上面的代码中,可以看到中文注释的部分已经将其中部分参数的含义标注出来了。