2

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?

7
  • 1
    Please show the urls.py file. Commented Sep 7, 2015 at 15:36
  • Are you getting any errors running it? Commented Sep 7, 2015 at 15:37
  • don't you need to define model = Blog in the second one? Commented Sep 7, 2015 at 15:47
  • 5
    your blog_search url is never reached because blog_home is matched first for all urls starting with blog/ ...you need to fix your urls.py Commented Sep 7, 2015 at 16:06
  • 1
    Or add '$' at the end Commented Sep 7, 2015 at 16:07

1 Answer 1

3

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'),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.