Python 使用HTML运行Flask环境:接收预期else语句的错误消息
我收到如下错误消息:TemplateSyntaxError:模板意外结束。Jinja正在寻找以下标签:“elif”或“else”或“endif”。需要关闭的最里面的块是“if” 我使用的是Linux环境,此代码应计算利率。有单独的文件用于运行脚本和函数。但是,一旦运行程序,我会在页面上收到一条错误消息,说明在以下代码之前有一条预期的else语句:Python 使用HTML运行Flask环境:接收预期else语句的错误消息,python,html,flask,jinja2,Python,Html,Flask,Jinja2,我收到如下错误消息:TemplateSyntaxError:模板意外结束。Jinja正在寻找以下标签:“elif”或“else”或“endif”。需要关闭的最里面的块是“if” 我使用的是Linux环境,此代码应计算利率。有单独的文件用于运行脚本和函数。但是,一旦运行程序,我会在页面上收到一条错误消息,说明在以下代码之前有一条预期的else语句: return render_template('interest_form.html', calc_total=True).
return render_template('interest_form.html', calc_total=True).
{% if total %}<h4>Total amount is</h4>
<textarea name="Amount" rows="5" cols="20"> {{total}}</textarea>
{% endif %}
</form>
以下是完整代码:
from server import app, valid_time
from flask import request, render_template
from Calculator import Calculator
@app.route("/", methods=["POST", "GET"])
def interest_total():
if request.method == 'POST':
initial=float(request.form["initial"])
rate=float(request.form["rate"])
time=float(request.form["time"])
Calculator.total_interest(initial,rate,time)
total=Calculator.total_interest()
return render_template('interest_form.html', total=total)
return render_template('interest_form.html', calc_total=True)
@app.route('/time', methods=["POST", "GET"])
def time_interest():
if request.method == 'POST':
initial=float(request.form["initial"])
rate=float(request.form["rate"])
total=float(request.form["total"])
Calculator.time_required(initial,rate,total)
time=Calculator.time_required()
return render_template('interest_form.html', time=time)
return render_template('interest_form.html', calc_time=True)
@app.route('/credits', methods=['GET'])
def credits():
return render_template('credits.html')
我正在尝试使用html表单发送输入:
<!doctype.html>
<head><h2>Interest</h2><head>
<html>
<body>
<div>
<form action="routes.py" method='POST'>
<div style="margin: 10px 0px">
<label>Amount Invested ($): </label><br/>
<input name="initial" placeholder="Amount Invested"/>
</div>
<div style="margin: 10px 0px">
<label>Interest Rate (%): </label><br/>
<input name="rate" placeholder="Amount Invested"/>
</div>
<div style="margin: 10px 0px">
<label>Time Investment (Years): </label><br/>
<input name="time" placeholder="Amount Invested" {%if calc_time %}disabled{% endif %}/>
</div>
<div style="margin: 10px 0px">
<label>Total Interest ($): </label><br/>
<input name="total" placeholder="Amount Invested" {%if calc_total %}disabled{% endif %}/>
</div>
<div style="margin: 10px 0px">
<button type="submit">Submit</button>
</div>
<div><p>{{initial}} and {{time}}</p></div>
{% if total %}<h4>Total amount is<h4>
<textarea name="Amount" rows="5" cols="20"> {{total}} </textarea>
</form>
</div>
<div>
<a href="file:///tmp_amd/cage/export/cage/4/project/templates/interest_form.html/time">Time Form</a>
<a href="file:///tmp_amd/cage/export/cage/4/project/templates/interest_form.html">Total Form</a>
<br/><a href="/credits">Credits Page</a>
</div>
</body>
</html>
兴趣
投资额(美元):
利率(%):
时间投资(年):
利息总额(美元):
提交
{{initial}}和{{time}
{%if total%}总金额为
{{total}}
我使用了一个不同的模板代码,它具有相似的函数和返回格式,在运行它时似乎没有问题
(编辑:我已更新以下代码):
{%if total%}总金额为
{{total}}
{%endif%}
但是,错误消息现在显示:jinja2.exceptions.TemplateSyntaxError:模板意外结束。Jinja正在寻找以下标签:“endblock”。需要关闭的最里面的块是“块”。如果模板中的块:
{% if total %}
<h4>Total amount is<h4>
<textarea name="Amount" rows="5" cols="20"> {{total}</textarea>
</form>
{% endif %}
{%if total%}
总金额为
{{total}
{%endif%}
编辑后更新的答案:
您还需要在文件末尾添加一个
{%endblock%}
,以关闭文件顶部的{%block content%}
。我收到了一个类似的错误-原因是一个{%block content%}
,我认为它不会受到影响,因为它在HTML中被注释掉了-结果是罪魁祸首
详情:
jinja2.exceptions.TemplateSyntaxError:模板意外结束。Jinja正在查找以下标记:“endblock”。需要关闭的最内层块是“block”
我知道每个{%block content%}
都需要一个{%endblock%}
,但我不知道的是,如果你在{%block content%}
周围有HTML注释,它就不起作用(也就是说,它实际上没有得到注释)
只是把它放在这里,以防有人像我一样挣扎了很久才弄明白。
下面是我的html代码,虽然有人对此进行了评论,但正是罪魁祸首给了我这个例外。
`
从注释代码中删除
{%block content%}
解决了它。行计算器中的错误缩进。所需时间(初始、速率、总计)
只是一个打字错误,对吗?是的!编辑好了!您好。现在错误显示:jinja2.exceptions.TemplateSyntaxError:模板意外结束。Jinja正在查找以下标记:“endblock”。需要关闭的最里面的块是“block”。它现在显示错误消息,如上面问题的编辑部分所述。There现在似乎有两个错误。每个{%block content%}
段都必须用{%endblock%}
关闭。在末尾添加一个。另外,为什么要将整个网页放入块中?您的base.html
是否为空?base.html
应该(在大多数情况下)是一个完整的html文档,您可以使用{%block…%}{%endblock%}将内容插入其中
@GAEfan谢谢!!刚刚意识到这个问题。至于base.html,它应该只包含文件的标题作为规范的一部分。@Harris,编辑后更新答案。如果它解决了您的问题,请随意接受:)
<!--
{% block content %}
<table>
<tr valign='top'>
<td> <img src="{{ user.avatar(128) }}"></td>
-->