首页 > 解决方案 > 无法在 django 2.0 中扩展模板

问题描述

在阅读了两次文档和大量伪教程后,我被 Django 中的模板生成/扩展所困扰。

我的项目结构是:

root
   project
      templates
          base.html
   app
      templates
          child.html
   static
      imgs
          logo.png

在我的 settings.js 上:

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',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
            ],
        },
    },
]

我的base.html:

{% load static %}
<html>
<head>
    <title>BioPy</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    <img src="{% static "imgs/logo.png" %}" alt="BioPy Logo">
</head>
<body>
    <div class="container">
      <div class="row justify-content-center">
        <div class="col-8">
          {% block content %} {% endblock %}
        </div>
      </div>
    </div>
</body>
</html>

我的孩子.html:

{% extend "base.html" %}
{% block content %}
     <h1 class="mt-2">Registration</h1>
     <hr class="mt-0 mb-4">
     {% load crispy_forms_tags %}
        <form method="post">
            {% csrf_token %}
            {{ form|crispy }}
            <button type="submit">Sign Up</button>
        </form>
{% endblock %}

我收到以下错误:

在此处输入图像描述

在简化示例中,BioPy 是项目,BioPyApp 是应用程序,registration.html 是子项。

此致,

标签: djangodjango-templates

解决方案


您正在编写无效的语法,它不是{% extend "base.html" %},它是{% extends "base.html" %}


推荐阅读