Categories
Python Answers

How to get query string parameters with Python Django?

Spread the love

Sometimes, we want to get query string parameters with Python Django.

In this article, we’ll look at how to get query string parameters with Python Django.

How to get query string parameters with Python Django?

To get query string parameters with Python Django, we can use the request.GET property.

For instance, we write

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

in the get_item view function to get the id query parameter with

id = int(request.GET.get('id'))

And we get the type query parameter with

type = request.GET.get('type', 'default')

The type query parameter has default value 'default' if it doesn’t exist.

Conclusion

To get query string parameters with Python Django, we can use the request.GET property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *