【发布时间】:2021-07-30 16:50:09
【问题描述】:
我正在学习 Django,我偶然发现了一个问题: 是否可以从 helloworld.html 的 {% extends "app/base.html" %} 链接中创建 base.html 文件的路径 文件尽可能短? 所以我只需要写 {% extends "base.html" %} 来让它工作。
现在,如果我尝试这样做,python 将在 def helloworld(request): views.py 中的函数处抛出异常。
所以我的应用文件结构是:
├── app
│ ├── migrations
│ ├── static
│ ├── templates
│ ├── app
│ ├── base.html
│ ├── helloworld.html
│ ├── views.py
│ └── models.py
├── DjangoProject
│ ├── __init__.py
│ ├── urls.py
│ ├── settings.py
│ ├── templates
├── manage.py
helloworld.html:
{% extends "app/base.html" %}
{% block head_title %} hello {% endblock %}
{% block content %}
<p> hello world </p>
{% endblock %}
views.py:
from django.shortcuts import render
from django.http import HttpRequest
def helloworld(request):
hello_title = 'Test'
assert isinstance(request, HttpRequest)
return render(
request,
'app/helloworld.html',
{
'title_test':hello_title
}
)
settings.py:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
'app',]
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',
],
},
},
]
我尝试在 settings.py 文件中的 TEMPLATES 部分的 'DIRS:' 部分中包含另一个路径,但它没有似乎工作。 我发现的 django 文档和另一个 stackoverflow 帖子只讨论了添加“app/base.html”路径。
【问题讨论】:
标签: python-3.x django path django-templates