I have a dictionary which has all the country codes and the corresponding country names, a sample of it looks like this:
{'AF': 'Afghanistan'}
{'AL': 'Albania'}
{'DZ': 'Algeria'}
{'AD': 'Andorra'}
{'AO': 'Angola'}
When i followed this stack overflow question: How to iterate through a list of dictionaries in Jinja template? to try and iterate through the countries I had an issue as it's not adding any elements. This is my code:
{% extends "base.html" %} {% block title %}Test{% endblock %}
{% block content %}
<div class="container pt-5">
<h1 align="center">TEST PAGE</h1>
</div>
{% for dict_item in countries %}
{% for key,value in dict_item.items %}
<h1>Key: {{ key }}</h1>
<h2>Value: {{ value }}</h2>
{% endfor %}
{% endfor %}
{% endblock %}
It's not adding any headings and when i tried dict_items.items()
(with brackets after items), I got an error of: jinja2.exceptions.UndefinedError: 'str object' has no attribute 'items'
I'm not too sure what's going wrong. Any help would be much appreciated.
(Just incase it's useful, this is my views.py:)
@views.route("/test", methods=["GET"])
@login_required
def test():
countries = Country.query.all()
for country in countries:
countriess = {}
countriess[country.country_code] = country.country_name
print(countriess)
return render_template("TEST.html", user=current_user, countries=countriess)