Python Flask:无法使python flask变量对html模板文件中的另一个if块可见,因为" with“语句范围以if块结尾。尝试将if块放在for循环的上方,但它不起作用。
key:value列表类似于:"{ subject : Maths,UnitTest1-Score: 90,UnitTest2-Score: 95,UnitTest2-Score: 98 }“希望将subject_name变量传递给第二个if语句"if key == 'UnitTest3'”,表示有兴趣引用相应的主题,然后更新数据库中的标记。由于变量是在第一个if语句"if key == 'Subject‘%}“中定义的(使用'with’语句)。尝试了不同的方法,但是上下文在第一个if块之后没有向前移动。尝试在第4行"if key”之后添加"with subject_name='‘“,以便它可以在第二行if "if key == 'Subject'”中可见,但是它会给出错误,如果在第二行if“if key == 'Subject'”使用,则主题是未定义的。我相信jinja2支持这一点。感谢任何帮助。
{% block content %}
{% for i in all_subjects %}
<article>
{%for key, value in i.items() %}
{% if key %}
{% if key == 'Subject' %}
{% with subject_name = value %}
{% with subject = subject_db.find_one({'subject_name': subject_name}) %}
{% if subject %}
<div>
<a href="#" >{{subject['subject_name']}}</a>
</div>
{% endif %}
{% endwith %}
{% endwith %}
{% endif %}
{% if key == 'UnitTest3' %}
{% with marks = value %}
<form action="" method='POST'>
<div>
<label> Marks: <input type="number" name="total_marks" min="0" value=0 oninput="this.value = Math.abs(this.value)"> </label>
<button type="submit">Update</button>
</div>
</form>
{% endwith %}
{% endif %}
{% endif %}
{% endfor %}
</article>
{% endfor %}
{% endblock content %}
https://stackoverflow.com/questions/56649487
复制相似问题