Django Context data


Sometimes, your template need a bit more of information. For example, we would like to have the user in the header of the page, with a link to their profile next to the logout link. In these cases, use the get_context_data method.

class BookView(DetailView):
    template_name = "book.html"

    def get_context_data(self, **kwargs)
        """ get_context_data let you fill the template context """
        context = super(BookView, self).get_context_data(**kwargs)
        # Get Related publishers
        context['publishers'] = self.object.publishers.filter(is_active=True)
        return context

You need to call get_context_data method on the super class and it will return the default context instance. Any item that you add to this dictionary will be available to the template.


<h3>Active publishers</h3>
  {% for publisher in publishers %}
    <li>{{ }}</li>
  {% endfor %}