gpt4 book ai didi

python - 基于 Listview 类的 View 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 01:01:54 25 4
gpt4 key购买 nike

我在我的应用程序中使用基于类的 View ,但我卡在了某一点。我正在使用 ListView 并创建了两个类,它们是 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 没有过滤。我也添加了打印语句但没有被调用。

我可以在同一个文件中使用 ListView 创建两个类吗?这是问题所在吗?

最佳答案

您需要终止您的 blog/ URL 条目。在没有终止的情况下,它会匹配所有以“blog/”开头的 URL,包括“blog/search”,因此不会有任何请求到达 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'),

关于python - 基于 Listview 类的 View 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32442074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com