Heem
Heem

Reputation: 123

Listview Class based view is not working properly

I am using class based views in my app but I am stuck at one point. I am using ListView and created two classes which are sub-classes of ListView.

views.py

class blog_home(ListView):
    paginate_by = 3
    model= Blog
    context_object_name = 'blog_title'
    template_name = 'blog.html'

class blog_search(ListView):
    paginate_by = 4

    context_object_name = 'blog_search'
    template = 'blog_search.html'

    def get_queryset(self):
        self.search_result = Blog.objects.filter(title__contains = 'Static')
        return self.search_result

urls.py

urlpatterns = [
url(r'^$', index, name='index'),
url(r'^grappelli/', include('grappelli.urls')),
url(r'^blog/', blog_home.as_view(), name='blog_home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/search/',blog_search.as_view(),name='blog_search'),
]

In my above code in blog_Search(), get_queryset() method is not getting called. I mean it's not working. If I use same same method in blog_home it does work.

blog_search not filtering. I added print statement also but not get called.

Can I create two classes with ListView in same file? Is this the problem?

Upvotes: 2

Views: 90

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

You need to terminate your blog/ URL entry. Without termination, it matches all URLs beginning with "blog/", including "blog/search", so no requests ever make it to the blog_search view.

url(r'^blog/$', blog_home.as_view(), name='blog_home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/search/$',blog_search.as_view(),name='blog_search'),

Upvotes: 3

Related Questions