我收到如下错误消息:TemplateSyntaxError:模板意外结束。 Jinja 正在寻找以下标签:“elif”或“else”或“endif”。需要关闭的最里面的块是“if”。
我使用的是 Linux 环境,这段代码应该计算利率。运行脚本和函数有单独的文件。但是,一旦运行该程序,我就会在页面上收到一条错误消息,指出在以下内容之前有一个预期的 else 语句代码:
return render_template('interest_form.html', calc_total=True).
下面是完整的代码:
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>
我使用了一个不同的模板代码,它具有类似的功能和返回格式,运行它时似乎没有问题。但是,这似乎显示了上述错误消息。
(编辑:我已经更新了以下代码):
{% if total %}<h4>Total amount is</h4>
<textarea name="Amount" rows="5" cols="20"> {{total}}</textarea>
{% endif %}
</form>
然而,错误消息现在显示:jinja2.exceptions.TemplateSyntaxError: Unexpected end of template。 Jinja 正在寻找以下标签:'endblock'。需要关闭的最里面的块是“块”。
最佳答案
您需要关闭 if
阻止您的模板:
{% if total %}
<h4>Total amount is<h4>
<textarea name="Amount" rows="5" cols="20"> {{total}</textarea>
</form>
{% endif %}
编辑后更新答案:
您还需要一个
{% endblock %}
在文件末尾关闭 {% block content %}
在文件的顶部。
关于python - 使用 HTML :receiving error message of expected else statement 运行 Flask 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49822676/