为什么不为我加载jinja继承的头部?

时间:2017-02-21 00:45:27

标签: python templates jinja2 template-inheritance

我正在尝试对以下两个页面进行模板继承,并且所有内容都是继承的,但是我的页面标题。我已经尝试了将标题放在不同位置的几种变体,但仍然没有。顺便说一句,我在我的本地主机上工作。

这是我的代码:

base.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    {% block head %}
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>{% block title %}{% endblock %} - My Webpage</title>
        <link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/cerulean/bootstrap.min.css" rel="stylesheet" integrity="sha384-zF4BRsG/fLiTGfR9QL82DrilZxrwgY/+du4p/c7J72zZj+FLYq4zY00RylP9ZjiT" crossorigin="anonymous">
    {% endblock %}
</head>
<body>
    <div class="container">
        {% block content %} {% endblock %}
    </div>
</body>
</html>

的login.html

{% extends "base.html" %}
{% block head %}{% endblock %}
{% block title %} Log In {% endblock %}
{% block content %}
    <h2>Log In</h2>
    <a href="/signup">Sign Up</a>
    <form method="post">
        <label for="username">Username:</label>
        <input id="username" type="text" name="username" value="{{username}}">
        {{username_error}}
        {{user_not_found}}
        <br>
        <label for="pass">Password:</label>
        <input id="pass" type="password" name="password" value="">
        {{incorrect_password}}
        {{password_error}}
        <br>
        <button>Submit</button>
    </form>
{% endblock %}

2 个答案:

答案 0 :(得分:0)

原来我只需要取出头部和标题部分的头部块来加载。之前我没有这么做的原因是因为我有另一个模板必须使用标题中的一些特定的CSS。

{% extends "base.html" %}
{% block title %} Log In {% endblock %}
{% block content %}
    <h2>Log In</h2>
    <a href="/signup">Sign Up</a>
    <form method="post">
        <label for="username">Username:</label>
        <input id="username" type="text" name="username" value="{{username}}">
        {{username_error}}
        {{user_not_found}}
        <br>
        <label for="pass">Password:</label>
        <input id="pass" type="password" name="password" value="">
        {{incorrect_password}}
        {{password_error}}
        <br>
        <button>Submit</button>
    </form>
{% endblock %}

答案 1 :(得分:0)

@Bill先生,或只使用{% block head %}{% endblock %}

{% block head %}{{ super() }}{% endblock %}

不客气:)

相关问题