Get nested dict items using Jinja2 in Flask

MattO

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.

Nava

I think you want to know how access the nested dict in template

If you think I got your question

Generally, This is the way to access the nested dictionary items in dictionary.

If the iterables are getting nested further just you have to increase the forloop depth level whether it is list or dict.

Here I am giving just a generic example in my own way for your understanding

Data:

parent_dict = {1: {'A':'val1','B':'val2'}, 2:{'C':'val3','D':'val4'}}

iteration in jinja2:

{% for key,parent_dict_item in parent_dict.items() %}
   {% for key2, nested_value in parent_dict_item.items() %}
      <li><a href = "{{ nested_value }}">{{ nested_value }}</a> </li>
   {% endfor %}
{% endfor %}

Answer:

<li><a href="val1">val1</a> </li>
<li><a href="val2">val2</a> </li>
<li><a href="val3">val3</a> </li>
<li><a href="val4">val4</a> </li>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Ansible Jinja2: How to get nth value of nested dict?

How do I append static items to a dict using Jinja2

Extract nested dict value with conditions from JSON using JINJA2 in Ansible

Using .get() in Jinja2?

How to get dict key value format using Jinja2 in ansible using debug or set fact module

Using sass with Flask and jinja2

Get information from a Nested Json response using Python3 and jinja2

Flask with Jinja2 - Passing value from Flask to Javascript using Jinja2 statements

ansible - create list of dict using jinja2

Get count of list items that meet a condition with Jinja2

How can I get the base url with Flask / Jinja2?

store html checkbox value into list using flask, jinja2

jinja2 flask: using rowspan twice while iterating

Finding the sum of numbers in jinja2 template using flask

Passing HTML to template using Flask/Jinja2

How to autoreload Jinja2 templates using nginx, uswgi and flask?

how to get nested undeclared variables in python jinja2

How to add nested groupby using Jinja2 templates

Ansible: How to create nested dictionary using Jinja2

Using jinja2 and python 3, get undefined variable error

Combine nested dict items to a string

Increment items in nested python dict

Sort dict of dict in jinja2 loop

Nested For Loop in Jinja2

using dict to connect items

How to get current URL in jinja2/flask (request.url not working)

Populate a form using Flask / Jinja2 template. Input text fields are truncating data

How can you change the color of a jinja2 entry depending on some condition? (using flask)

How do I view a session variable using Flask/Jinja2?