1

This is my current url path is as http://localhost:8000/api/projects/abcml/2021 and in the urls.py page I am passing as path("api/projects/<str:project_handle>/<int:year>", functionname...) and in the view, I accept this parameters with self.kwargs.get method.

I want to pass url as this format http://localhost:8000/api/projects/abcml/?year=2021 What changes do I need to make in url pattern?

I tried this path("api/projects/<str:project_handle>/?year=<int:year> but did not seem correct also in the view page, instead of self.kwargs.get I changed it to self.request.query_params.get for year parameter. That did not work either. Error it throwing is Page not found (404).

1 Answer 1

3

The query string [wiki] is not part of the path, and therefore can not be matched.

You thus specify as path:

path('api/projects/<str:project_handle>/', functionname)

In the view, you can access the data with self.request.GET['year'] and this will return a string or a KeyError in cas the year was not provided in the query string.

Sign up to request clarification or add additional context in comments.

6 Comments

Its throwing 'argument of type 'Request' is not iterable'
@HardikPatil: likely the error is somewhere else, for example by iterating over a request object somehow...
How to check whether this query parameter year is empty or not in the view ?
@HardikPatil: if 'year' in self.request.GET.
Yeah also along with that, had to check 'if not' condition inside 'if in' condition. Hey thank you! Btw how did you add code block section in the comment?
|

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.