Reputation: 53
When I am using function based view using the following code:
from django.views import View from django.views.generic import TemplateView, ListView
from .models import Restaurant
def restaurant_listview(request):
template = 'restaurants/restaurants_list.html'
context = {
"queryset" : Restaurant.objects.order_by('-updated')
}
return render (request, template, context)
it is working with the url file kept as follows:
from django.conf.urls import url
from django.contrib import admin
from restaurants import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^restaurants/$', views.restaurant_listview),
]
But when I am trying to do the same thing with class based views it is not working only the following portion doesn't seem to work:
<ul>
{% for obj in queryset %}
<li>{{obj.name}}, {{obj.location}}, {{obj.category}}, {{obj.timestamp}}</li>
{% endfor %}
</ul>
the following part works fine:
{% extends 'base.html' %}
{% block title %}
Restaurants List {{ block.super }}
{% endblock %}
{% block content %}
<h1>Restaurants</h1>
for class based view my views.py is:
class RestaurantListView(ListView):
queryset = Restaurant.objects.all()
template_name = 'restaurants/restaurants_list.html'
and urls.py is:
url(r'^restaurants$', RestaurantListView.as_view(), name='Home')
P.S. I am following this guide : https://www.youtube.com/watch?v=yDv5FIAeyoY&t=25471s
Upvotes: 0
Views: 1267
Reputation: 308899
For a list view, you should change the template to:
{% for obj in restaurant_list %}
Or, if you really want to use the variable queryset
in the template, then set context_object_name
.
class RestaurantListView(ListView):
queryset = Restaurant.objects.all()
template_name = 'restaurants/restaurants_list.html'
context_object_name = 'queryset'
Upvotes: 1