我需要使用Django设置可选参数。我尝试了在Stack Overflow上看到的方法,但是没有用。
我的代码:
views.py:
def get(self, request, id_document, optional_parameters = 'owner'):
#code here
urls.py:
url(r'^getdoclist/(?P<id_document>[^/]+)/$', Get_DocumentList.as_view()),
url(r'^getdoclist/(?P<id_document>[^/]+)/(?P<owner>[^/]+)/$', Get_DocumentList.as_view()),
使用此方法无效:(
感谢您能帮助我
您需要使用这样的可选参数:
def get(self, request, id_document, owner=None): # owner=any other value
或
def get(self, request, id_document, **kwargs):
owner = self.kwargs.get('owner', None)
或者,您可以考虑URL querystring。然后,您不需要在urls.py
中定义两个url,但是仍然可以获取该值,如下所示:
def get(self, request, id_document, **kwargs):
owner = request.GET.get('owner')
然后您需要像这样调用网址:
localhost:8000/getdoclist/1234567890/?owner=SomeOne
^^^^^^^^^^^ ^^^^^^^^^^^^^
document id owner value after question mark