基本上,当我运行烧瓶程序并提交web表单时,我会收到以下错误消息:
[2021-11-10 19:12:16,207] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/Users/jed/Dropbox/Code/Python Project/server.py", line 13, in home
return render_template("results.html", result=result)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/flask/templating.py", line 148, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jinja2/environment.py", line 1068, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jinja2/environment.py", line 997, in get_template
return self._load_template(name, globals)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jinja2/environment.py", line 958, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jinja2/loaders.py", line 137, in load
code = environment.compile(source, name, filename)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jinja2/environment.py", line 757, in compile
self.handle_exception(source=source_hint)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "/Users/jed/Dropbox/Code/Python Project/templates/results.html", line 18, in template
{% if data[value] is "T": %}
jinja2.exceptions.TemplateSyntaxError: expected token 'name', got 'string'
127.0.0.1 - - [10/Nov/2021 19:12:16] "POST / HTTP/1.1" 500 -
以下是所有涉及的文件:
server.py(服务器(:
from flask import Flask, render_template, redirect, request, url_for
from forms import QuizForm
app = Flask(__name__)
app.config["SECRET_KEY"] = "1234-asdf-!@#$-ASDF"
@app.route("/", methods=["GET", "POST"])
def home():
form = QuizForm()
if form.is_submitted():
result = request.form
return render_template("results.html", result=result)
return render_template("quiz.html", form=form)
if __name__ == '__main__':
app.run()
forms.py(表单类(:
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import InputRequired
class QuizForm(FlaskForm):
name = StringField("First Name:", validators=[InputRequired()])
asa = StringField("You can prove a triangle congruent using ASA (T/F):", validators=[InputRequired()])
sas = StringField("You can prove a triangle congruent using SAS (T/F):", validators=[InputRequired()])
aaa = StringField("You can prove a triangle congruent using AAA (T/F):", validators=[InputRequired()])
sss = StringField("You can prove a triangle congruent using SSS (T/F):", validators=[InputRequired()])
aas = StringField("You can prove a triangle congruent using AAS (T/F):", validators=[InputRequired()])
ssa = StringField("You can prove a triangle congruent using SSA (T/F):", validators=[InputRequired()])
submit = SubmitField("Submit!")
base.html(html模板(:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="{{ url_for('static',filename='JavaScript Libraries/jquery-ui-1.13.0.custom/jquery-ui.min.css') }}">
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/main.css') }}">
<script src="{{ url_for('static',filename='JavaScript Libraries/jquery-3.6.0.min.js') }}"></script>
<script src="{{ url_for('static',filename='JavaScript Libraries/jquery-ui-1.13.0.custom/jquery-ui.min.js') }}"></script>
<script src="{{ url_for('static',filename='js/main.js') }}"></script>
<title>Python Quiz Project</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
quiz.html(表单(:
{% extends "base.html" %}
{% block content %}
<div id="h1t"><h1>Geometry Quiz</h1></div>
<form id="quiz" action="" method="post">
<h2 id="desc">This is a quiz/survey about triangle congruence, <br>this form will ask you to provide your name and answer several questions about proving triangles congruent, <br>and will give you your score afterwards.</h2><br>
<h3>So first off we'll just start of with your first name. <br>What should I call you?</h3><br>
{{form.name.label}}<br>
{{form.name(size=30)}}<br><br>
<h3>Alright, so now that we've got that done with, let's get right into the questions.</h3><br>
{{form.asa.label}}<br>
{{form.asa(size=30)}}<br><br>
{{form.sas.label}}<br>
{{form.sas(size=30)}}<br><br>
{{form.aaa.label}}<br>
{{form.aaa(size=30)}}<br><br>
{{form.sss.label}}<br>
{{form.sss(size=30)}}<br><br>
{{form.aas.label}}<br>
{{form.aas(size=30)}}<br><br>
{{form.ssa.label}}<br>
{{form.ssa(size=30)}}<br><br>
{{form.submit()}}
</form>
{% endblock %}
results.html(表单后面的页面,包含结果(:
{% extends "base.html" %}
{% block content %}
<table border="1">
<tr>
<th>Question</th>
<th>Correct Answer</th>
<th>Your Answer</th>
<th>Score</th>
</tr>
{% for key,value in result.items() %}
{% if key is asa %}
<tr>
<td>{{data[key]}}</td>
<td>T</td>
<td>{{data[value]}}</td>
<td>
{% if data[value] is "T": %}
1/1
{% else: %}
0/1
{{% endif %}}
</td>
</tr>
{% elif key is sas %}
<tr>
<td>{{data[key]}}</td>
<td>T</td>
<td>{{data[value]}}</td>
<td>
{% if data[value] is "T" %}
1/1
{% else %}
0/1
{{% endif %}}
</td>
</tr>
{% elif key is aaa %}
<tr>
<td>{{data[key]}}</td>
<td>F</td>
<td>{{data[value]}}</td>
<td>
{% if data[value] is "F" %}
1/1
{% else %}
0/1
{{% endif %}}
</td>
</tr>
{% elif key is sss %}
<tr>
<td>{{data[key]}}</td>
<td>T</td>
<td>{{data[value]}}</td>
<td>
{% if data[value] is "T" %}
1/1
{% else %}
0/1
{{% endif %}}
</td>
</tr>
{% elif key is aas %}
<tr>
<td>{{data[key]}}</td>
<td>T</td>
<td>{{data[value]}}</td>
<td>
{% if data[value] is "T" %}
1/1
{% else %}
0/1
{{% endif %}}
</td>
</tr>
{% elif key is ssa %}
<tr>
<td>{{data[key]}}</td>
<td>F</td>
<td>{{data[value]}}</td>
<td>
{% if data[value] is "F" %}
1/1
{% else %}
0/1
{{% endif %}}
</td>
</tr>
{% endif %}
{% endfor %}
</table>
{% endblock %}
我似乎不明白错误消息告诉我的是什么,请帮忙!
在results.html
模板中,您有多个语法错误:
-
result
变量是ImmutableMultiDict
,例如[('asa', 'T'), ('sas', 'F'), ('aaa', 'T')]
。因此,在迭代过程中,key
循环变量将字段名称作为简单字符串(例如asa
(接收,而value
变量也将字段值作为字符串(例如,T
(保存。您应该使用这些变量,而不是未定义的data
变量(您在home
视图中将提交的表单作为result
而非"数据"传递。 -
密钥检查应该作为字符串比较来完成。因此,您必须编写
{% if key is eq("asa") %}
(在这里我们使用eq
运算符(,或者简单地编写{% if key == "asa" %}
,而不是{% if key is asa %}
。 -
{{% endif %}}
不正确,只需使用单个{}
:{% endif %}
即可。
校正后的results.html
模板:
{% extends "base.html" %}
{% block content %}
<table border="1">
<tr>
<th>Question</th>
<th>Correct Answer</th>
<th>Your Answer</th>
<th>Score</th>
</tr>
{% for key,value in result.items() %}
{% if key == "asa" %}
<tr>
<td>{{ key }}</td>
<td>T</td>
<td>{{ value }}</td>
<td>
{% if value == "T" %}
1/1
{% else %}
0/1
{% endif %}
</td>
</tr>
{% elif key == "sas" %}
<tr>
<td>{{ key }}</td>
<td>T</td>
<td>{{ value }}</td>
<td>
{% if value == "T" %}
1/1
{% else %}
0/1
{% endif %}
</td>
</tr>
{% elif key == "aaa" %}
<tr>
<td>{{ key }}</td>
<td>F</td>
<td>{{ value }}</td>
<td>
{% if value == "F" %}
1/1
{% else %}
0/1
{% endif %}
</td>
</tr>
{% elif key == "sss" %}
<tr>
<td>{{ key }}</td>
<td>T</td>
<td>{{ value }}</td>
<td>
{% if value == "T" %}
1/1
{% else %}
0/1
{% endif %}
</td>
</tr>
{% elif key == "aas" %}
<tr>
<td>{{ key }}</td>
<td>T</td>
<td>{{ value }}</td>
<td>
{% if value == "T" %}
1/1
{% else %}
0/1
{% endif %}
</td>
</tr>
{% elif key == "ssa" %}
<tr>
<td>{{ key }}</td>
<td>F</td>
<td>{{ value }}</td>
<td>
{% if value == "F" %}
1/1
{% else %}
0/1
{% endif %}
</td>
</tr>
{% endif %}
{% endfor %}
</table>
{% endblock %}