Django and query string parameters


Assuming I have a 'get_item' view, how do I write the URL pattern for the following php style of URL?

(I am not using the standard 'nice' type of URL ie: as it is not practical)

Specifically, how do I make the view respond when the user types the above in a browser, and how do I collect the parameters and use it in my view?

I tried to at least get the id part right but to no avail. The view won't run when I type this in my browser

My url pattern:

(r'^get_item/id(?P<id>\d+)$', get_item)

My view:

def get_item(request):
    id = request.GET.get('id', None)

In short, how do I implement Php's style of url pattern with query string parameters in django?

6/12/2019 9:47:48 PM

Make your pattern like this:

(r'^get_item/$', get_item)

And in your view:

def get_item(request):
    id = int(request.GET.get('id'))
    type = request.GET.get('type', 'default')

Though for normal detail views etc. you should put the id/slug in the url and not in the query string! Use the get parameters eg. for filtering a list view, for determining the current page etc...

9/14/2010 6:44:08 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow