Django and query string parameters

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…

Leave a Comment