[Django]-Django and query string parameters

73👍

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')

Django processes the query string automatically and makes its parameter/value pairs available to the view. No configuration required. The URL pattern refers to the base URL only, the query string is implicit.

For normal Django style, however, you should put the id/slug in the base URL and not in the query string! Use the query parameters eg. for filtering a list view, for determining the current page etc…

22👍

You just need to change your url pattern to:

(r'^get_item/$', get_item)

And your view code will work. In Django the url pattern matching is used for the actual path not the querystring.

👤Nathan

Leave a comment