django override admin 在本地工作而不是在生产环境中工作
django override admin works locally not on production
我的 django 10 项目中有以下目录结构:
/my-project/ # project dir
+app1
+templates
+admin
base.html
404.html
500.html
我的模板属性在设置中如下所示:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'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',
'common.context_processors.site_info',
],
},
},
]
我的自定义 base.html 显示在我的本地计算机上。当我将其上传到我的生产服务器时,它不再覆盖并使用项目文件夹中的 base.html 文件。
我更改了建议的应用顺序 here and tried printing the dirs element of the templates attribute which prints "templates/" like here。
有谁知道如何让它在我的生产环境中运行?
您必须在设置中使用绝对路径以避免出现问题。例如:
import os
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..', '..')
# depending where your settings.py live
...
'DIRS': [
os.path.join(PROJECT_ROOT, 'templates'),
],
我的 django 10 项目中有以下目录结构:
/my-project/ # project dir
+app1
+templates
+admin
base.html
404.html
500.html
我的模板属性在设置中如下所示:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'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',
'common.context_processors.site_info',
],
},
},
]
我的自定义 base.html 显示在我的本地计算机上。当我将其上传到我的生产服务器时,它不再覆盖并使用项目文件夹中的 base.html 文件。
我更改了建议的应用顺序 here and tried printing the dirs element of the templates attribute which prints "templates/" like here。
有谁知道如何让它在我的生产环境中运行?
您必须在设置中使用绝对路径以避免出现问题。例如:
import os
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..', '..')
# depending where your settings.py live
...
'DIRS': [
os.path.join(PROJECT_ROOT, 'templates'),
],