Home > Enterprise >  Django: Passing parameters in URL as query arguments
Django: Passing parameters in URL as query arguments

Time:07-02

How can I pass parameters via URL as query parameters to avoid multiple and complicated url patterns?

For example, instead of making a complicated url like example.com/page/12/red/dog/japan/spot or something like that, and then a corresponding entry in urls.py that will parse that url and direct it to a view, I want to simply get a url where I can freely add or remove parameters as needed similar to the ugly way example.com/page?id=12&color=red&animal=dog&country=Japan&name=spot

Then in urls.py simply have something like

path('page/<parameter_dictionary>', views.page, name='page' parameters='parameter_dictionary)

If I have to use url patterns, how can I account for urls that have parameters that may or may not fit the pattern, such as sometimes

"/page/12/red/dog/Japan/spot" -> path('page/<int:id>/<str:color>/<str:animal>/<str:country>/<str:name>', views.page, name='page'),
"/page/12/dog/red/Japan/"-> path('page/<int:id>/<str:animal>/<str:color>/<str:country>', views.page, name='page')
"/page/dog/red/Japan/"-> path('page/<str:animal>/<str:color>/<str:country>', views.page, name='page')

I would like to just have anything sent to http://example.com/page/ go to views.page(), and then be accessible by something like

animal = request.GET['animal']
color = request.GET['color']
id = request.GET['id']

etc. so examples below would all work via one entry in urls.py

example.com/page?id=12&animal=dog&country=Japan&name=spot 
example.com/page?id=12&color=red&animal=dog&name=spot
example.com/page?id=12&country=Japan&color=red&animal=dog&name=spot 

CodePudding user response:

You are looking for queryparameters and you are almost done with it. The following code is untested but should kinda work:

def page(request):
    animal = request.GET.get("animal",None) # default None if not present
    color = request.GET.get("color",None)
    return render(request,'some_html.html')
# urls.py:
path('page/', views.page, name='page')

You access the queryparameters via the passed request object request.GET. This is a dict like object. The main difference is that this object handles multi keys, for example if you pass the these params ?a=1&a=2 to your url, it converts request.GET.get("a") # Returns ["1","2"]. Read more about QueryDict here.

Also be sure to know the difference and best practice for url parameters and queryparameters. Example Stackoverflow post

  • Related