如何在 Django 3.1 中添加 "templates " 和 "statics" 文件夹
How can add "templates " and "statics" folder in Django 3.1
setting.py
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
#TEMPLATES_DIR = path.join(BASE_DIR, 'templates') #it dosen't work
#STATICS_DIR = path.join(BASE_DIR, 'statics') #it also dosen't work
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '0#nf@nm5jb0)meuq&j6kztt534@nhk)k&$zh=#emcxi7_7fo'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'musicianapp',
]
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 = 'musicianList.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'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 = 'musicianList.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/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.1/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.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATICS_DIR,
]
请注意 top 和 3rd top 行与 Djago2.7 版本的主要区别所在[= =39=]
In Djnago2.7 where imported os as below,
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__)))
and I used the below code for connecting templates and statics forlder
TEMPLATES_DIR =os.path.join(BASE_DIR,"templates")
STATICS_DIR =os.path.join(BASE_DIR,"statics")
But there is differece in form Djnago 3.1 from pathlib imported Path
given below,
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
and the os techinques dosen't works as below
TEMPLATES_DIR =os.path.join(BASE_DIR,"templates")
STATICS_DIR =os.path.join(BASE_DIR,"statics")
如何连接模板和静态文件夹,需要帮助。
注意: 我在主项目中创建了模板和静态文件夹,其中已经显示了 manage.py 和 db.sqlite3 文件。在我的项目方向的屏幕截图下方,
如果您查看 settings.py
文件的顶部,您会看到 BASE_DIR
正在使用 pathlib
:
BASE_DIR = Path(__file__).resolve().parent.parent
因此,您不再需要 os
加入路径,您只需执行以下操作:
STATICS_DIR = BASE_DIR / "statics"
这是 3.1 中的新功能 - 您可以在此处的发行说明中阅读相关信息:https://docs.djangoproject.com/en/3.1/releases/3.1/
我也推荐 https://docs.djangoproject.com/en/4.0/howto/overriding-templates/,但如果没有任何反应,那应该是重构问题,尤其是在使用 PyCharm 时。因此,您需要还原更改。它对我有用。
setting.py
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
#TEMPLATES_DIR = path.join(BASE_DIR, 'templates') #it dosen't work
#STATICS_DIR = path.join(BASE_DIR, 'statics') #it also dosen't work
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '0#nf@nm5jb0)meuq&j6kztt534@nhk)k&$zh=#emcxi7_7fo'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'musicianapp',
]
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 = 'musicianList.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'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 = 'musicianList.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/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.1/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.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATICS_DIR,
]
请注意 top 和 3rd top 行与 Djago2.7 版本的主要区别所在[= =39=]
In Djnago2.7 where imported os as below,
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__)))
and I used the below code for connecting templates and statics forlder
TEMPLATES_DIR =os.path.join(BASE_DIR,"templates")
STATICS_DIR =os.path.join(BASE_DIR,"statics")
But there is differece in form Djnago 3.1
from pathlib imported Path
given below,
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
and the os techinques dosen't works as below
TEMPLATES_DIR =os.path.join(BASE_DIR,"templates")
STATICS_DIR =os.path.join(BASE_DIR,"statics")
如何连接模板和静态文件夹,需要帮助。
注意: 我在主项目中创建了模板和静态文件夹,其中已经显示了 manage.py 和 db.sqlite3 文件。在我的项目方向的屏幕截图下方,
如果您查看 settings.py
文件的顶部,您会看到 BASE_DIR
正在使用 pathlib
:
BASE_DIR = Path(__file__).resolve().parent.parent
因此,您不再需要 os
加入路径,您只需执行以下操作:
STATICS_DIR = BASE_DIR / "statics"
这是 3.1 中的新功能 - 您可以在此处的发行说明中阅读相关信息:https://docs.djangoproject.com/en/3.1/releases/3.1/
我也推荐 https://docs.djangoproject.com/en/4.0/howto/overriding-templates/,但如果没有任何反应,那应该是重构问题,尤其是在使用 PyCharm 时。因此,您需要还原更改。它对我有用。