I am trying to build a detail view in django, but nothing is displayed in the template.
views.py
class MyDetailView(DetailView):
model = Article
template_name = 'detail.html'
detail.html
{% extends 'base.html' %}
{% load i18n %}
{% endblock %}
{% block content %}
{% for item in itemlist %}
{{item.pk}}
{{item.title}}
{% empty %}
There are no items in this list
{% endfor %}
{% endblock %}
Why is nothing displayed in the template here?
You do not pass a item with the name
itemlist
to the template in aDetailView
. In aListView
(which looks more appropriate), it will by default use a context variable namedobject_list
. Since the variable is not present, the{% for … %}…{% endfor %}
template block [Django-doc] will be resolved to the empty string.If you want to pass the queryset of
Article
s to the context through theitemlist
name, you can set thecontext_object_name
attribute [Django-doc]: