pythondjangodjango-templatesdjango-template-filters

How to assemble the variable name to use data from JSONField inside the Template in django?


I'm trying to build a website for tv series using Django framework, I put in the models.py all kinds of details about that show and a JSONField (named 'episodes') to define the number seasons and episodes in each season.
Example: { "1" : 15 , "2" : 25 , "3" : 23}
where season 1 contains 15 episodes, 2 contains 25 and so on

the problem starts when I tried making a dropdown in the template where the series is playing so the user can select the episode to watch: This Code is working:

    Season 1
    {% with ''|center:show.episodes.1 as range %}
    {% for episode in range %}
        Episode {{forloop.counter}}
    {% endfor %}
    {% endwith %}

    Season 2
    {% with ''|center:show.episodes.2 as range %}
    {% for episode in range %}
        Episode {{forloop.counter}}
    {% endfor %}
    {% endwith %}

    Season 3
    {% with ''|center:show.episodes.3 as range %}
    {% for episode in range %}
        Episode {{forloop.counter}}
    {% endfor %}
    {% endwith %}

But surely this can be much cleaner if I could use 2 for loops inside each others one for the seasons and one for the episodes which sounds easy but for some reason I couldn't do it.
I tried:

{% for season in show.episodes %}
    Season {{forloop.counter}}
    {% for episode in show.episodes.forloop.counter %}
        Episode {{forloop.counter}}
    {% endfor %}
{% endfor %}

yes it is that show.episodes.forloop.counter line stopping me.
that definitely won't work, I tried doing:

{% for season in show.episodes %}
    Season {{forloop.counter}}
    {% for episode in show.episodes|add:forloop.counter %}
        Episode {{forloop.counter}}
    {% endfor %}
{% endfor %}

But that doesn't work because I am trying to add the number to the already loaded show.episodes which is the dict of the JSON (Error)

I also tried doing a custom filter like in this Question : How to assemble a variable name in Django templating language? but looks like the same thing is happening: adding the number to the already loaded show.episodes which is the dict of the JSON (Error)


I know it seems very easy but for 2 days I have been trying to figure out a solution and I couldn't, So If you can, Please help me. I am quite a beginner btw :)


Solution

  • show.episodes = { "1" : 15 , "2" : 25 , "3" : 23}
    

    Propose to iterate through your dict this way:

    {% for season, series in show.episodes.items %}
        Season {{ season }}
        
               {% for _ in ''|center:series %}
                    Episode {{forloop.counter}}
               {% endfor %}
    
    {% endfor %}
    

    It is ok for not big dictionaries. To process bigger dicts I propose to use custom template filters or tags. Or move processing logic to view function.

    Docs for filter and tags https://docs.djangoproject.com/en/3.2/howto/custom-template-tags/#custom-template-tags-and-filters