python - 为什么我没有加载 jinja 继承的头部部分?

标签 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 %}

最佳答案

结果我只需要取出头部 block 即可加载头部和标题部分。我之前没有这样做的原因是因为我有另一个模板必须使用 header 中的某些特定 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 %}

关于python - 为什么我没有加载 jinja 继承的头部部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42356732/

相关文章:

python - 将嵌套字典中的 numpy 数组转换为列表,同时保留字典结构

python - 防止意外的标准输入读取并锁定子进程

python - 删除 pandas 数据框中的行

c++ - g++ 在编译时走得太远

c++ - C++ 中嵌套模板函数的 const 限定符

python - 从 Twitter 渲染 HTML 显示代码而不是嵌入的推文

python - 如何从蓝图模板扩展基础 Flask Jinja 模板?

jinja2 - 如何从 Jinja 列表变量 (DBT) 中删除括号

python可以编码为utf-8但不能解码

具有模板成员变量的 C++ 类