[Django]-Trying to pass two dictionaries but only first one works

1👍

You can only have one context dictionary, but a dictionary can have as many key/values as you want.

 def home(request):
      context = {'title': 'HOME','post': posts}
      return render(request, 'blog/home.html',context)
👤arjun

1👍

What about this one?

{
    'title': 'HOME',
    'post': posts
}

This way, both variables are part of the same object.

It’s crucial to put both values to the same data structure, because in your code, post was a fourth parameter, which is reserved for a different functionality.

1👍

You can only pass one dictionary as the context for the page.

def home(request):
    context = {'title': 'HOME',
               'post': posts }
    return render(request, 'blog/home.html', context)

1👍

Django’s render function takes only one positional argument for context. that’s why your first dictionary is working and the second one is chopped off as Django take fourth positional argument as content_type

render(request, template_name, context=None, content_type=None, status=None, using=None)

ref : https://docs.djangoproject.com/en/2.2/topics/http/shortcuts/

So you should pass only one object of dictionary with all your desired data. it can have nested object.

 def home(request):
    context_data = {'title': 'Home',
                   'post': posts }
    return render(request, 'blog/home.html', context_data)

Leave a comment