2023年5月更新:
templates
文件夹可以放在
django-project
文件夹下,或者放在每个
app
文件夹下,如下所示:
django-project
|-core
│ |-settings.py
│ └-urls.py
|-app1
| |-urls.py
| └-views.py
|-app2
| |-urls.py
| └-views.py
└-templates # Here
|-app1
| └-a1.html
└-app2
└-a2.html
或者:
django-project
|-core
│ |-settings.py
│ └-urls.py
|-app1
| |-urls.py
| |-views.py
| └-templates # Here
| └-app1
| └-a1.html
└-app2
|-urls.py
|-views.py
└-templates # Here
└-app2
└-a2.html
例如,首先您需要在
settings.py
中将
"app1"
和
"app2"
设置为
INSTALLED_APPS,如下所示:
INSTALLED_APPS = [
...
"app1",
"app2",
]
那么如果 templates
文件夹就在 django-project
文件夹下面,如下所示:
django-project
|-core
│ |-settings.py
│ └-urls.py
|-app1
| |-urls.py
| └-views.py
|-app2
| |-urls.py
| └-views.py
└-templates # Here
|-app1
| └-a1.html
└-app2
└-a2.html
然后,您需要将 BASE_DIR / 'templates'
设置为"DIRS"中的TEMPLATES在settings.py
中如下所示。*我建议将 templates
文件夹放在 django-project
文件夹的下面,如上所示,因为您可以轻松管理模板:
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
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",
],
},
},
]
如果templates
文件夹就在每个app
文件夹下面,如下所示:
django-project
|-core
│ |-settings.py
│ └-urls.py
|-app1
| |-urls.py
| |-views.py
| └-templates # Here
| └-app1
| └-a1.html
└-app2
|-urls.py
|-views.py
└-templates # Here
└-app2
└-a2.html
然后,您需要保持"DIRS"
为空(这是默认值),而不将BASE_DIR/'templates'
设置为"DIRS"
,如下所示,在settings.py
中的TEMPLATES
中:
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",
],
},
},
]
然后,在
app1/views.py
和
app2/views.py
中定义
test()
如下所示。请注意,您需要在
render()中设置
"app1/a1.html"
和
"app2/a2.html"
而不是仅仅设置
"a1.html"
和
"a2.html"
,如下所示:
from django.shortcuts import render
def test(request):
return render(request, "app1/a1.html")
from django.shortcuts import render
def test(request):
return render(request, "app2/a2.html")
然后,在
app1/urls.py
和
app2/urls.py
中,将每个
app1
和
app2
的
test
视图设置如下所示:
from django.urls import include, path
from . import views
app_name = "app1"
urlpatterns = [
path("", views.test, name='test'),
]
from django.urls import include, path
from . import views
app_name = "app2"
urlpatterns = [
path("", views.test, name='test'),
]
然后,在
core/urls.py
中设置
app1
和
app2
的每个
urls.py
,如下所示,然后
app1
和
app2
的模板将被渲染而不会出现任何错误:
from django.urls import include, path
urlpatterns = [
...
path("app1/", include('app1.urls')),
path("app2/", include('app2.urls'))
]
最后,我再次建议将
templates
文件夹放在
django-project
文件夹下面,如下所示,因为您可以轻松地在一个地方管理模板:
django-project
|-core
│ |-settings.py
│ └-urls.py
|-app1
| |-urls.py
| └-views.py
|-app2
| |-urls.py
| └-views.py
└-templates # Here
|-app1
| └-a1.html
└-app2
└-a2.html