无法在 Django 中扩展模板

时间:2021-04-29 01:50:40

标签: django django-templates

我有以下模板:

{% 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>

而且我还有一个模板来扩展它:

{% extends "registration/login.html" %}

{% block testblock %}
    <div> testblock content </div>
{% endblock testblock %}

settings.py 中的模板设置是标准设置,未更改:

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',
            ],
        },
    },
]

在我看来,我所做的一切都与文档一致,我无法弄清楚其中哪些不起作用。老实说,我试图找出原因,并在这里和谷歌上搜索答案,但我失败了。请帮帮我。

链接到 GitHub repository 如果您需要查看整个项目结构或 templates folder

2 个答案:

答案 0 :(得分:0)

我看不到您的完整 settings.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',
            ],
        },
    },
]

答案 1 :(得分:0)

我只是忘记将相应视图的 render() 函数中的模板名称从 "registration/login.html" 更改为 "registration/test.html"。太傻了,麻烦的借口。

相关问题