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.
Answers:
Your function "rooms" has the same name as the list "rooms".
Changing the function or list name can resolve the problem.
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.
Your function "rooms" has the same name as the list "rooms".
Changing the function or list name can resolve the problem.