How to specify something other than pk or slug for DetailView

≯℡__Kan透↙ 提交于 2019-12-05 03:13:16

A slug doesn't have any particular significance in Django. It's just a name for a field that identifies a row. If your slug is called something else, eg name, just specify name as the slug_field attribute in your view subclass.

If you need something more complicated, you can always override get_object in the view class.

You can use attribute "pk_url_kwarg"


urls.py

url(r'^mymodel/(?P<name>\d+)/$', MyDetailView.as_view())


views.py

class MyDetailView(LoginRequiredMixin, DetailView):
    model = ModelName
    pk_url_kwarg = "name" # primary key(to identify object uniquely)
    template_name = "template.html"

for reference view uml design http://epydoc.pythondiary.com/generic-views/ (click on detail view)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!