0

In my views.py I have a function dataPrint(), which returns page, and dictionary categories_zipped. But when I try to use this dictionary in my skills.html file using Django syntax {% for category in categories_zipped %} it works only for the first time, when I try to use it later, it does not work. (I'm not getting any errors, the second use, just returns nothing, as shown on the images below)

def dataPrint(request):
    categories = [
        'all',
        'javascript',
        'html',
        ]
    categories_menu = [
        'All',
        'JS',
        'HTML',
        ]
return render(request, 'skill/skills.html', {'categories_zipped': zip(categories, categories_menu})

skills.html

 {% for category in categories_zipped %}
      <a class="link-icon" href="/skills/{{category.0}}">
        <img
          class="small-icon"
          src="{% static '/skillsgraph/img/' %}{{category.0}}.svg"
          alt="{{category.0}}'.svg'"
        />
        <span class="icon-title">{{category.1}}</span>
      </a>
      {% endfor %}
      <select name="category" class="link-list">
        
        {% for cat in categories_zipped %} 
        <option value="{{ cat.0 }}">{{ cat.1 }}</option>
        {% endfor %}
      </select>

It renders only for the first time and renders only the first thing. If I change order (put select before a), it works only for select, but not for a's.

enter image description here

enter image description here

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
wloszynski
  • 37
  • 6
  • How do you create `category_zipped` in your view function? If it's a generator, you can only iterate over it one time. If you need to iterate over it multiple times, you should use some other type, for example a list. This can be done by using the `list()` builtin in the view function. For example `categories_zipped=list(categories_zipped)` – Håken Lid Dec 11 '20 at 11:23
  • The result of `zip` has the same issue as in the linked duplicate (also applies to, for example, file objects). – Karl Knechtel Dec 11 '20 at 11:26
  • using list(categories_zipped), solved the problem, thank you very much. – wloszynski Dec 11 '20 at 11:28

1 Answers1

1

Construct list from zip

return render(request, 'skill/skills.html', {'categories_zipped': list(zip(categories, categories_menu}))
iklinac
  • 14,944
  • 4
  • 28
  • 30