'for loop of ListView not iterating through data in Django
Solution 1:[1]
You didn't define the model name on the view.Add the model name and try to add custom context_object_name. For reference check this
class BookListView(generic.ListView):
model = Book
context_object_name = 'my_book_list' # your own name for the list as a template variable
queryset = Book.objects.filter(title__icontains='war')[:5] # Get 5 books containing the title war
template_name = 'books/my_arbitrary_template_name_list.html' # Specify your own template name/location
Solution 2:[2]
You have to specify which model to create ListView. Here in your case define model=Post as
from django.views.generic.list import ListView
class PostList(ListView):
model = Post
template_name = 'blog/index.html'
queryset = Post.objects.filter(status=1).order_by("-created_on")
Or you can also use get_queryset() as
from django.views.generic.list import ListView
class PostList(ListView):
# specify the model
model = Post
template_name = 'blog/index.html'
def get_queryset(self, *args, **kwargs):
qs = super(PostList, self).get_queryset(*args, **kwargs)
qs = qs.filter(status=1).order_by("-created_on")
return qs
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pranta chakraborty |
| Solution 2 | user8193706 |
