Why is this for loop not working in my django template?

Question:

this is my views :

rooms = [
    {'id': 1, 'name': 'room-1'},
    {'id': 2, 'name': 'room-2'},
    {'id': 3, 'name': 'room-3'},
    {'id': 4, 'name': 'room-4'},
]

def rooms(request):
    return render(request, 'rooms.html', {'rooms': rooms})

and template codes :

{% for room in rooms %}
    <li>{{room.id}} -- {{room.name}}</li>
{% endfor %}

unfortunately for loop is not working.

Asked By: Pablo

||

Answers:

Your function "rooms" has the same name as the list "rooms".

Changing the function or list name can resolve the problem.

Answered By: Ali
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.