Reputation: 7
I am a beginner in django, and its version is 1.11.6
,as well as the python version I use is 3.6
.
I am studying generic view, generic.ListView
does not return any values.
views.py
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'Latest_question_list'
def get_queryset(self):
return Question.objects.order_by('-pub_date')[:5]
urls.py
from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultView.as_view(), name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$',views.vote, name='vote'),
]
the output of above code is:
no polls are available
the html page is included the following code:
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>no polls are available</p>
{% endif %}
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}"/>
unfortunately, I could not get the reason of the error.
Upvotes: 0
Views: 65
Reputation: 308779
The problem is that you have context_object_name = 'Latest_question_list'
(with a capital L) in your view, which does not match {% if latest_question_list %}
(all lowercase) in your template.
Change either the view or template so that they match. The PEP 8 style guide would recommend latest_question_list
, so I would change the view:
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
Upvotes: 3