无法在Django中扩展模板
我有以下模板:无法在Django中扩展模板,django,django-templates,Django,Django Templates,我有以下模板: {% load i18n %} {% load static %} <!DOCTYPE html> <html> <head> <title>{% translate "login page" %}</title> <link rel="stylesheet" type="text/css" href="{% static '
{% load i18n %}
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>{% translate "login page" %}</title>
<link rel="stylesheet" type="text/css" href="{% static 'taskmanager/style/authpages.css' %}">
</head>
<body>
{% block testblock %}
{% endblock testblock %}
<form method="post" action="{% url 'login' %}" class="main-form">
{% csrf_token %}
<label for="username">{% translate "User name: " %}</label>
{{ form.username }}
{{ form.username.errors }}
<label for="password">{% translate "Password: " %} </label>
{{ form.password }}
{{ form.password.errors }}
{% if form.errors %}
{{ form.non_field_errors }}
{% endif %}
<input type="submit" value="{% translate 'sign in' %}">
<input type="hidden" name="next" value="{{ next }}">
<a href="{% url 'register' %}">{% translate "Register" %}</a>
</form>
</body>
</html>
在我看来,我所做的每一件事都与文档一致,我无法确定哪一件不起作用。我诚恳地试图找出原因,并在这里和谷歌上寻找答案,但我失败了。请帮帮我
如果您需要查看整个项目结构,请链接到GitHub,或者我看不到您的完整设置.py,但我认为问题出在那里。您需要添加实际目录。下面是我的一个项目中的一个示例,只需复制/粘贴到您的项目上,它就会修复它:
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',
],
},
},
]
我只是忘记了将相应视图的render()函数中的模板名称从
的“registration/login.html”
更改为的“registration/test.html”
。这太傻了,麻烦你了。根据文档,DIRS是包含位于应用程序模板文件夹之外的模板的文件夹列表。若APP_DIRS参数设置为True,则模板加载器应该在每个APP的每个模板文件夹中搜索模板,并且由于我将tampletes存储在APP_name/templates文件夹中,所以加载器应该会看到它们。因此,DIRS参数用于通过添加额外的directory来扩展模板源。无论如何,我都试图这么做,并明确指向app_name/templates dir,但它没有改变任何内容。我的文件
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',
],
},
},
]
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',
],
},
},
]