Flask报错jinja2:expected token 'end of statement block', got 'as'

xuzhefeng
  • 3
新手上路,请多包涵

用Flask写,到了往前端页面 render_template()数据这一步,突然开始作死式的报错,怎么调后端都没用;哪位大神帮忙找出原因,谢谢
报错:

clipboard.png

前端模板:“index.html”

clipboard.png

{% extends "base.html" %}
{% extends "bootstrap/wtf.html" as wtf %}
{% block title %} Hello {% endblock %}
{% block page_content %}
<div class="page-header">
    <h1>Hello,{% if name %}{{ name }}{% else %}Stranger{% endif %}!</h1>
</div>
{{ wtf.quick_form(form) }}
{% endblock %}

后端文件:"Hello.py"

from flask import Flask
from flask import render_template
from flask_bootstrap import Bootstrap
from flask_moment import Moment
# from datetime import datetime
# from flask import abort
from flask_script import Manager
from flask_wtf import Form
from wtforms import StringField,SubmitField
from wtforms.validators import DataRequired

app = Flask(__name__)
app.config["SECRET_KEY"] = "hard to guess string" #设置Flask-WTF
manager = Manager(app)
bootstrap = Bootstrap(app)  #初始化Flask-Boorstrap
moment = Moment(app)        #初始化Flask-Moment
#定义表单类
class NameForm(Form):
    name = StringField("What is your name?",validators=[DataRequired()])
    submit = SubmitField("Submit")




@app.errorhandler(404)
def page_not_found(e):
    return render_template("404.html"),404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template("500.html"),500

@app.route('/',methods = ["GET","POST"])
def index():
    name = None
    form = NameForm()
    if form.validate_on_submit():
        name = form.name.data
        form.name.data = ""
    return render_template("index.html")

if __name__ == '__main__':
# app.run(host = "0.0.0.0")
    app.run(debug = True)
回复
阅读 6.2k
1 个回答
✓ 已被采纳

{% extends "bootstrap/wtf.html" as wtf %}改成:

{% import "bootstrap/wtf.html" as wtf %}

参考《Flask Web开发实战》第四章

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏