Python Flask: Unable to make the python flask variable visible to another if block within the html template file as the "with" statement scope ends with if block. Tried to place the if block above the for loop but it does not take.
The key:value list has something like this: "{subject: Maths, UnitTest1-Score: 90, UnitTest2-Score: 95,UnitTest2-Score: 98 }" Would like to pass the subject_name variable to second if statement "if key == 'UnitTest3'" as interested in referring the respective subject and then update marks in the database. Since the variable is defined (with 'with' statement)in the first if statement " if key == 'Subject' %}. Tried different ways however the context does not move forward after the first if block. Tried to add a "with subject_name='' " after 4th line "if key" so that it can be visible in second if "if key == 'Subject'" however it gives error that the subject_name is undefined if used in second if "if key == 'Subject'" I believe jinja2 supports this. Any help appreciated.
{% 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 %}