for this dictionary with this Flask controller
projects = {
'life-calc':{'url':'life-calc',
'title': 'Life Calculator'},
'text-game':{'url':'text-game',
'title':'Text Adventure'},
'fill-it-up':{'url':'fill-it-up',
'title':'Fill It Up'},
'rock-paper-scissors':{'url':'rock-paper-scissors',
'title':'Rock, Paper, Scissors'},
'bubble-popper':{'url':'bubble-popper',
'title':'Bubble Popper'}
}
@app.route('/')
def index():
return render_template("index.html",
projects = projects)
and the template as such
<h1>
List of My Projects
</h1>
<ol>
<li>
<a href = "life-calc">Life Calculator</a>
</li>
<li>
<a href = "text-game">Adventure Game</a>
</li>
<li>
<a href = "fill-it-up">Fill It Up</a>
</li>
<li>
<a href = "rock-paper-scissors">Rock Paper Scissors</a>
</li>
<li>
<a href = "bubble-popper">Bubble Popper</a>
</li>
</ol>
<p>test section below</p>
<ol>
{% for project in projects %}
<li><a href = "{{ project['url'] }}">{{ project['title'] }}</a> </li>
{% endfor %}
</ol>
{% endblock %}
How can I access the items in the dict to print a list of my projects as in the HTML above the test?
I solved my own problem with help from Rendering a python dict in Jinja2 / Werkzeug
The template block should be
{% for key, value in projects.iteritems() %}
<li><a href={{value['url']}}>{{value['title']}}</a></li>
{% endfor %}
But I'm still curious as to how to access further nested dictionaries, and if this is the smartest way to create a simple menu.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…