网址.py
url(r'^customer/(?P<name>[^\s]+)/$', customerDetailView.as_view(), name="customerDetailView"), #pass 'name' variable
和
视图.py
class customerDetailView(DetailView):
context_object_name = 'customerDetail'
template_name = "customer.html"
allow_empty = True
def __init__(self, name=None, *args):
self.name = name # name is 'None'... Why...?
def get_queryset(self):
return Customer.objects.get(name=self.name)
我只是请求'192.168.1.5/customer/abc/',但没有'name'...
如何接收“姓名”?我该怎么办?
编辑 - - -
视图.py
class customerDetailView(DetailView):
context_object_name = 'customerDetail'
template_name = "customer.html"
allow_empty = True
"""
def __init__(self, **kwargs):
import pdb;pdb.set_trace()
self.name = kwargs['name']
"""
def get_queryset(self):
# import pdb;pdb.set_trace()
self.name = self.kwargs['name'] # Thanks Kay Zhu!!
return Customer.objects.get(name=self.name)
我应用您回答的代码。
然后,我得到一个错误
Generic detail view customerDetailView must be called with either an object pk or a slug.
所以我需要'pk'...
我该怎么办?