Basically, when I run my flask program and I submit my web form, I get the following error message:
[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 -
Here are all of the files involved:
server.py (server):
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 (form class):
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 template):
<!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 (form):
{% 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 (page after the form, containing results):
{% 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 %}
I can't seem to understand what the error message is telling me, please help!
CodePudding user response:
In the results.html
template you have multiple syntax errors:
The
result
variable is anImmutableMultiDict
e.g.[('asa', 'T'), ('sas', 'F'), ('aaa', 'T')]
. Therefore during the iteration thekey
loop variable receives the field's name as a simple string (e.g.asa
), and thevalue
variable will hold the field's value as a string as well (e.g.T
). You should use these variables, not the undefineddata
variable (you pass the submitted form asresult
not 'data' in thehome
view.The key checking should be done as string comparison. So instead of
{% if key is asa %}
you have to write{% if key is eq("asa") %}
(where we use theeq
operator), or simply{% if key == "asa" %}
.The
{{% endif %}}
is incorrect, just use a single{}
:{% endif %}
.
The corrected results.html
template:
{% 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 %}