pythondjango

How to render different dict into Django Views


I've been learning django recently and I am struggling when sending over 1 dictionary into a template in Django

I have my embedViews.py which has multiple classes according to different sections of the page (So the views.py doesn't look a mess. And I can pass 1 of the classes into a template (html file) however, there is a page where I need to pass multiple dictionaries (lists) into the template. My logic told me to pass it in this way

rooms = {
'rooms': ev.Rooms.rooms
}

documents = {
    'docs': ev.Documents.documents
}

posts = {
    'posts': ev.Posts.posts
}
def home(request):
    return render(
        request,
        'index.html',
        {
        
            'rooms': rooms,
            'documents': documents,
            'posts': posts,
        }
)

But the home/ does not render correctly, what I am doing wrong? Couldn't find the solution in the documentation.

I expect to see all the lists on my html file. But it is showing in a strange way... Not the way I intended to look like.

Page View


Solution

  • rooms =  ev.Rooms.rooms
    
    
    documents =  ev.Documents.documents
    
    
    posts = ev.Posts.posts
    def home(request):
        return render(
            request,
            'index.html',
            {
            
                'rooms': rooms,
                'documents': documents,
                'posts': posts,
            }
    )
    

    Edit** To fix the issue, declare the rooms and documents as variables not as dictionaries.