django通用视图Generic detail view PublisherList must be called with either an object pk or a slug

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django通用视图Generic detail view PublisherList must be called with either an object pk or a slug相关的知识,希望对你有一定的参考价值。

view.py
class PublisherList(DetailView):
#通用视图参数
model = Publisher
context_object_name = 'publisher_list' #取名字,默认为object_list
queryset = Publisher.objects.all()
slug_field = 'publisher_list_slug'
template_name = 'helloTime.html'
def get_context_data(self,**kwargs): #重写DetailView类的方法
context = super(PublisherList,self).get_context_data(**kwargs)
context['book_list'] = Book.objects.filter(firstname__contains='jobs')
return context

url.py
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myTest2.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
(r'^admin/', include(admin.site.urls)),
#url(r'^hello/', 'myTest2.views.caluteTime'),
#url(r'^search/$','search')
(r'^hello/$', PublisherList.as_view()),
)
slug_field = 'publisher_list_slug'这个参数去掉,写错了

from django.views.generic import ListView
from apps import Departs

urlpatterns = patterns('',

#url(r'^generic_views/', OrdersListView.as_view(template_name='publisher_list_page.html')),
url(r'^generic_views/$',ListView.as_view(model=Departs,context_object_name='publisher_list',template_name='publisher_list_page.html')),

)

试试这个,model是列表的模型,context_obj是模板渲染的列表名称,用来循环的。例如 %p in publisher_list%,最后一个是模板。。

第一次回答问题,望采纳。
参考技术A Generic detail view PublisherList must be called with either an object pk or a slug
通用详细视图出版社列表必须以一个对象主键或者一个slug来调用,你看下url配置吧,还是主要看文档,django好久没碰了

以上是关于django通用视图Generic detail view PublisherList must be called with either an object pk or a slug的主要内容,如果未能解决你的问题,请参考以下文章

如何将object_id传递给Django中的通用视图object_detail

Django 通用类视图

更正 Django 模板 url 变量以与通用视图 list_detail 一起使用

重用 Django Rest Framework Generic 视图来获取它的 QuerySet

API Guide之Generic views

如何在django2.0.6中的通用删除视图中添加用户身份验证