Reputation: 25
I have an if-statement in a Jinja2 template in complete.html
, but it doesn't run the right code regardless if the if-condition is True or False.
I call a redirect url to another view function which then renders it, carrying the variable that determines the condition of the if-loop. I printed out the condition and it's correct, but the if-loop doesn't run the correct code. Am I missing something that is causing the if-statement to not work correctly.
My code:
view.py
@app.route('/', methods=['GET', 'POST'])
def home():
form = CourseForm()
if form.validate_on_submit():
course_name = form.cname.data.upper()
course_id = form.cid.data
course_sec = form.sec.data.upper()
email = form.email.data.lower()
reserved = form.reserved.data
result = checkCourse(course_name, course_id, course_sec)
if result is not None:
flash('Unable to find course')
else:
known = False
course = Course.query.filter_by(cname=course_name, cid=course_id, sec=course_sec).first()
if course is None:
course = Course(cname=course_name, cid=course_id, sec=course_sec)
db.session.add(course)
db.session.flush()
user = User(email=email, course_id=course.id, reserved=reserved)
db.session.add(user)
else:
ckey = course.id
result = checkUser(email, ckey, reserved)
if result != None:
known = True
else:
user = User(email=email, course_id=ckey, reserved=reserved)
db.session.add(user)
db.session.commit()
return redirect(url_for('complete', known=known))
return render_template('home.html', form=form)
@app.route('/complete')
def complete():
known = request.args.get('known', type=string)
app.logger.debug(known)
return render_template('complete.html', known=known)
Templates:
base.html
{% extends "bootstrap/base.html" %}
{% block title %}UBC Course Alert{% endblock %}
{% block styles %}
{{super()}}
<link rel="stylesheet"
href="{{url_for('.static', filename='home.css')}}">
{% endblock %}
{% block content %}
<div class="jumbotron">
<div class="container">
{% block page_content %}{% endblock %}
</div>
</div>
{% endblock %}
complete.html
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block page_content %}
<p>
{% if known %}
You already added this course. You'll receive a email as soon as there's space :)
{% else %}
Thanks for registering your course, you'll receive a email as soon as there's space!
{% endif %}
</p>
{% endblock %}
Upvotes: 2
Views: 4088
Reputation: 1121236
You converted the variable known
to a string; it'll contain either the string value 'True'
or 'False'
. These are not boolean values, but rather strings of either 4 or 5 characters long. Both strings then are seen as true values, as they are non-empty.
You'll either have to translate the string to a boolean, or leave the known
value empty to indicate a false value.
Converting back to boolean would be as easy as:
known = request.args.get('known', type=string) == 'True'
while using an empty value for false would look like this:
return redirect(url_for('complete', known='1' if known else ''))
and
known = request.args.get('known', default='', type=bool)
Upvotes: 1