我不能在 jinja 的 if 语句中使用变量
I cant use a variable inside an if statement in jinja
我正在尝试在 if
语句中使用参数,但它不起作用。我也尝试使用 {{ }}
.
@module002.route('/forum/<course_id>', methods=['GET', 'POST'])
@login_required
def module002_forum(course_id):
form = CommentForm()
if request.method == 'GET':
cursos = Follow.query.filter_by(user_id=current_user.id)
texto = Comment.query.filter_by(course_id=course_id).all()
id_mensaje = list(text.user_id for text in texto)
usuarios = User.query.filter(User.id.in_(id_mensaje)).all()
usuarios = {user.id: user for user in usuarios}
return render_template("module002_forum.html", module='module002', course_id=course_id,
form=form, cursos=cursos, texto=texto, user=current_user, usuarios=usuarios,db=get_db())
elif request.method == 'POST':
if current_user.is_authenticated and form.validate_on_submit():
comentario = Comment(user_id=current_user.id,course_id=course_id,comment=form.comment.data)
db.session.add(comentario)
db.session.commit()
flash("Comment added")
else:
flash("Error añadiendo comentario")
return redirect(url_for('module002.module002_forum', course_id=course_id))
{% extends 'base.html' %}
{% import "bootstrap/wtf.html" as wtf %}
{% block content %}
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="btn-group btn-group-justified">
{% for i in cursos %}
<a class={% if i.course_id==course_id %}"btn btn-primary"{%else%}"btn btn-primary active"{%endif%}
href="{{ url_for('module002.module002_forum', course_id=i.course_id) }}">{{
i.course_name }}</a>
{% endfor %}
</div>
</div>
</nav>
以下变体应该符合您的要求。
<a
class="btn btn-primary {{ ('','active')[i.course_id==course_id] }}"
href="{{ url_for('module002.module002_forum', course_id=i.course_id) }}"
>{{ i.course_name }}</a>
您还应该在 rule 中指定变量的类型,以确保比较有效。
@module002.route('/forum/<int:course_id>', methods=['GET', 'POST'])
在大括号和 else/endif 之间添加空格。必须是:
{% else %}
和 {% endif %}
而不是
{%else%}
和 {%endif%}
我正在尝试在 if
语句中使用参数,但它不起作用。我也尝试使用 {{ }}
.
@module002.route('/forum/<course_id>', methods=['GET', 'POST'])
@login_required
def module002_forum(course_id):
form = CommentForm()
if request.method == 'GET':
cursos = Follow.query.filter_by(user_id=current_user.id)
texto = Comment.query.filter_by(course_id=course_id).all()
id_mensaje = list(text.user_id for text in texto)
usuarios = User.query.filter(User.id.in_(id_mensaje)).all()
usuarios = {user.id: user for user in usuarios}
return render_template("module002_forum.html", module='module002', course_id=course_id,
form=form, cursos=cursos, texto=texto, user=current_user, usuarios=usuarios,db=get_db())
elif request.method == 'POST':
if current_user.is_authenticated and form.validate_on_submit():
comentario = Comment(user_id=current_user.id,course_id=course_id,comment=form.comment.data)
db.session.add(comentario)
db.session.commit()
flash("Comment added")
else:
flash("Error añadiendo comentario")
return redirect(url_for('module002.module002_forum', course_id=course_id))
{% extends 'base.html' %}
{% import "bootstrap/wtf.html" as wtf %}
{% block content %}
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="btn-group btn-group-justified">
{% for i in cursos %}
<a class={% if i.course_id==course_id %}"btn btn-primary"{%else%}"btn btn-primary active"{%endif%}
href="{{ url_for('module002.module002_forum', course_id=i.course_id) }}">{{
i.course_name }}</a>
{% endfor %}
</div>
</div>
</nav>
以下变体应该符合您的要求。
<a
class="btn btn-primary {{ ('','active')[i.course_id==course_id] }}"
href="{{ url_for('module002.module002_forum', course_id=i.course_id) }}"
>{{ i.course_name }}</a>
您还应该在 rule 中指定变量的类型,以确保比较有效。
@module002.route('/forum/<int:course_id>', methods=['GET', 'POST'])
在大括号和 else/endif 之间添加空格。必须是:
{% else %}
和 {% endif %}
而不是
{%else%}
和 {%endif%}