0

I have a var with the name 'seasons' that returns the number of seasons in a tv show and I know for a fact it does work and it returns it.

but I'm trying now to loop through it so if a tv show has 6 seasons it echos:

  • season1, season2.. etc

but it doesn't work.

code :

{% for season in range(seasons) %}
  <li rel="p{{season}}" class="activate-panel">الموسم {{ season }}</li>              
{% endif %}

reference to how I got the number of seasons from db:

I've an Episode model that has a field with the name season_number, so for order to get the higest season number I searched for the episode with the highest season number

latest_episode = Episode.objects.order_by('-season_number').filter(is_published=True, tvshow=tvshow).first()

then I get the season with

seasons = latest_episode.season_number

and it does return a number indeed.

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
A. Atiyah
  • 515
  • 1
  • 6
  • 16
  • Welcome to Stack Overflow! Please add your Django Model and Views code to your question so we can see how you are generating the `seasons` variable. – tatlar Mar 06 '19 at 23:01
  • You cannot use `range()` in templates. Check out [these](https://stackoverflow.com/questions/1107737/numeric-for-loop-in-django-templates) [questions](https://stackoverflow.com/questions/4831306/need-to-convert-a-string-to-int-in-a-django-template) for workarounds. – Endre Both Mar 06 '19 at 23:18
  • @tatlar hey thanks for caring i did edit the question hope its more clear now. :) – A. Atiyah Mar 06 '19 at 23:41
  • Did you figure it out? – Lord Elrond Mar 07 '19 at 03:11
  • yes I used the range methond inside the views.py instead of the template. – A. Atiyah Mar 07 '19 at 03:27

1 Answers1

0

This should fix your problem:

{% for season in seasons %}
  <li rel="p{{season}}" class="activate-panel">الموسم {{ season }}</li>              
{% endfor %}
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80