On certain pages I want my form drop down to be in a specific order and in other pages in a default order. When passing in a specific order from my app, my macro in form.html doesn't seem to see it. dropdown is never used, the template always displays the global data instead. Why is this happening?
form.html:
{% if dropdown %}
<select name="status">
{% for option in dropdown %}
<option value="{{ option }}">{{ option }}</option>
{% endfor %}
</select>
{% else %}
<select name="status">
{% for option in global_add_links_data()[0] %}
<option value="{{ option }}">{{ option }}</option>
{% endfor %}
</select>
{% endif %}
app.py:
dropdown = [
'Placed',
'Review Not Started',
'Review Passed',
'Review Failed',
'Contacted Pending',
'Contacted Failed',
'No Contacts',
'No Reply',
'Not Interested'
]
dropdown.insert(0, dropdown.pop(dropdown.index(link_status)))
return render_template('view.html', dropdown=dropdown)
You are not rendering
form.htmldirectly, you are renderingview.htmland importingform.html. When importing other templates, the template context is not passed by default.dropdownis local to theview.htmlcontext, so it is always undefined inform.html.To import a template with context, you use the
with contextkeywords.A better, more explicit way to handle this is to just pass
dropdownas an argument to the macro. This is more efficient, because as the docs above mention, imports are normally cached for performance, but usingwith contextdisables that.