Django template doesn't display dictionary elements

I have a dictionary named Rooms. I am sending the dictionary elements to home.html

Views.py

from django.shortcuts import render

# Create your views here.

rooms = [
    {'id': 1, 'name': 'Lets learn Python!'},
    {'id': 2, 'name': 'Front-End Developer'},
    {'id': 3, 'name': 'Back-End Developer '},    
]

def home(request):
    context = {'rooms': rooms}
    return render(request, 'base/home.html', context)

def rooms(request, pk):
    room = None
    for i in rooms:
        if i['id'] == int(pk):
            room = i
    context = {'room': room}
    return render(request, 'base/room.html', context) 

home.html

{% extends 'main.html' %}

{% block content %}
<h1>Home Template</h1>

<div>
    <div>
        {% for room in rooms %}
            <div>
                <h5>{{room.id}} -- <a href="/room/{{room.id}}">{{room.name}}</a></h5>
            </div>
        {% endfor %} 
    </div>
</div>

{% endblock content %}

Home.html displays <h5> text but does not display the dictionary elements.I have tried renaming context variable.

You need this bad boy room/<int:pk>/ in your urlpatterns

    from django.urls import path 
    from . import views  
    urlpatterns = [
        path('', views.home, name="Home"),
        path('room/<int:pk>/', views.rooms, name="Rooms")
    ]

I realised that rooms(dict) and rooms(func) was colliding. I just renamed the func to room!

Updated Views.py

from django.shortcuts import render

# Create your views here.

rooms = [
    {'id': 1, 'name': 'Lets learn Python!'},
    {'id': 2, 'name': 'Front-End Developer'},
    {'id': 3, 'name': 'Back-End Developer '},    
]

def home(request):
    context = {'rooms': rooms}
    return render(request, 'base/home.html', context)

def room(request, pk):
    room = None
    for i in rooms:
        if i['id'] == int(pk):
            room = i
    context = {'room': room}
    return render(request, 'base/room.html', context) 
Back to Top