Passing URL Parameters in Django

Question:
How can I create a URL with multiple parameters in Django’s urls.py? For example, I want a URL like this: /api/foodfeeds/?keywords=BURGER,teste&mood=happy&location=2323,7767.323&price=2
Solution 1:
As suggested by @Umair, you can pass the parameters as URL query parameters. You don’t need to mention them in your urls.py file. To access the data passed through the URL, you can use the following code snippet.
Solution 2:
The keys like keywords, mood, location, price, etc. are query parameters that should not be included in the URL. Instead, you can access them via request.query_params. For more information, refer to http://www.django-rest-framework.org/api-guide/requests/#query_params.
Question:
Can someone please explain how to pass parameters in a Django URL? I have a view called “def hours_ahead(request, offset)” in my views.py file to calculate the time ahead. My client wants to be able to:
– Enter a username and password in the URL with parameters for date and id and view available rooms based on what is entered.
– Enter a username, password, dates, and room code to book a room.
No interface is needed, just simple parameter passing through the URL.

Question:

As a beginner in Django, I am unsure which category my question pertains to. Despite my thorough search for
django post request
s and
parameter passing
, as well as consulting Django APIs, I have not yet found the solution I am seeking. My objective is to develop an API for my client, specifically using Django. While I am familiar with using http post, http get, and web services in .Net, I am unsure of the analogous process in Django. Ultimately, my client desires the ability to view:

  1. Access available rooms by entering date and id parameters along with your username and password in the url.
  2. To reserve a room, input your username, password, and the desired dates and room code.

Is it feasible to achieve parameter passing through URL without requiring an interface in Django? If so, could someone kindly guide me towards the appropriate solution?


Solution 1:

The parameters that have been captured are what you’re searching for.

The following code excerpt is derived from the aforementioned link.

# urls.py
from django.conf.urls import patterns, url
urlpatterns = patterns('blog.views',
    url(r'^blog/(?Pd{4})/$', 'year_archive', {'foo': 'bar'}),
)
# views.py
def year_archive(request, year, foo=None):
    # view method definition


Solution 2:


Django no longer includes “patterns” as of its 1.10 version.

Here is a 2019 Django 2.2 solution.

The currently suggested method for obtaining parameters through
regular expressions
is by using
re_path
. Refer to this link for more information: https://docs.djangoproject.com/en/2.2/ref/urls/#re-path.

# urls.py
from django.urls import re_path
from myapp import views
urlpatterns = [
    re_path(r'^blog/(?Pd{4})/$', 'year_archive', {'foo': 'bar'}),
]
# views.py
def year_archive(request, year, foo=None):
    # view method definition

Frequently Asked Questions