基于列表视图 Class 的视图无法正常工作
Listview Class based view is not working properly
我在我的应用程序中使用基于 class 的视图,但我卡在了某一点。我正在使用 ListView
并创建了两个 classes,它们是 ListView
.
的子 classes
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'),
]
在我上面的 blog_Search()
代码中,get_queryset()
方法没有被调用。我的意思是它不工作。如果我在 blog_home
中使用相同的方法,它确实有效。
blog_search 没有过滤。我也添加了 print 语句,但没有被调用。
我可以在同一个文件中创建两个 class 和 ListView
吗?是这个问题吗?
您需要终止您的 blog/
URL 条目。在没有终止的情况下,它会匹配所有以 "blog/" 开头的 URL,包括 "blog/search",因此没有任何请求会到达 blog_search 视图。
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'),
我在我的应用程序中使用基于 class 的视图,但我卡在了某一点。我正在使用 ListView
并创建了两个 classes,它们是 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'),
]
在我上面的 blog_Search()
代码中,get_queryset()
方法没有被调用。我的意思是它不工作。如果我在 blog_home
中使用相同的方法,它确实有效。
blog_search 没有过滤。我也添加了 print 语句,但没有被调用。
我可以在同一个文件中创建两个 class 和 ListView
吗?是这个问题吗?
您需要终止您的 blog/
URL 条目。在没有终止的情况下,它会匹配所有以 "blog/" 开头的 URL,包括 "blog/search",因此没有任何请求会到达 blog_search 视图。
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'),