Python jinja:TemplateSyntaxError:预期标记“名称”,得到“字符串”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15823989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:06:27  来源:igfitidea点击:

jinja: TemplateSyntaxError: expected token 'name', got 'string'

pythonflaskjinja2

提问by fox

Have two files in a Flask application:

在 Flask 应用程序中有两个文件:

base.html

base.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" href="../static/main.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/css/bootstrap.min.css">
    <title>Title</title>
</head>
<body>
<div id="content">
    {% marker "content" %}
</div>

</body>
</html>

upload.html, which extends base.html

upload.html, 延伸 base.html

{% extends "base.html" %}

{% block "content " %}
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
    <p><input type=file name=file>
        <input type=submit value=Upload>
</form>
{% endblock %}

I'm calling the latter in a view: return render_template('upload.html' ), and I'm getting an error:

我在 view: 中调用后者,return render_template('upload.html' )但出现错误:

jinja2.exceptions.TemplateSyntaxError
TemplateSyntaxError: expected token 'name', got 'string'

采纳答案by Sean Vieira

The issue is that {% block "content" %}should be {% block content %}- the block's name should not be quoted.

问题是{% block "content" %}应该是{% block content %}- 不应引用块的名称。

In addition the markerconstruct in your layout.html is not a valid Jinja2 tag- it should be {% block content %}{% endblock %}.

此外,markerlayout.html 中的构造不是有效的 Jinja2 标签——它应该是{% block content %}{% endblock %}.