0

I need to set optional parameters using Django. I try a method saw on Stack Overflow but it didn't work.

My code :

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()),

it didn't work using this method :(

Thanks if you could help me

1 Answer 1

1

You need to use opitional parameter like this:

def get(self, request, id_document, owner=None):  # owner=any other value

Or

def get(self, request, id_document, **kwargs):
   owner = self.kwargs.get('owner', None)

Alternativly, you can consider URL querystring. Then you do not need to define two urls in urls.py, but you can still get the value, like this:

def get(self, request, id_document, **kwargs):
   owner = request.GET.get('owner')

Then you need to call the url like this:

localhost:8000/getdoclist/1234567890/?owner=SomeOne
                         ^^^^^^^^^^^  ^^^^^^^^^^^^^
                         document id   owner value after question mark
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your anwer ! but when i set two parameters in the url it did'nt work
Sorry, what do mean by two parameters? Can you please add an example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.