Sentry Answers>Django>

How do I access parameters from the URL as part the HttpRequest object?

How do I access parameters from the URL as part the HttpRequest object?

Gareth D.

The problem

You are using URL parameters like https://example.com?first_name=Alex&sort=descend to pass data to your Django application and you want to access these as variables in your Django code. How can you extract this information from the Django HttpRequest object?

The solution

The simplest way is to access the parameters directly from Django’s request object using request.GET.get("first_name"). For example, if you have the following views.py file:

Click to Copy
from django.http import HttpResponse def home(request): first_name = request.GET.get("first_name") sort = request.GET.get("sort") text = f"<h1>Searched for first name: {first_name} with sort option: {sort}<h1>" return HttpResponse(text)

And the following in your urls.py file:

Click to Copy
from django.urls import path from . import views urlpatterns = [ path('', views.home) ]

Then you can run the development server and visit http://localhost:8000/?first_name=Alex&sort=descending to see the response “Searched for first name: Alex with sort option: descending”.

Getting parameters submitted in HTML forms

By default, HTML forms will submit data encoding in these GET parameters, using the name of any inputs as the keys of the parameters. Instead of typing out the parameters in the URL manually, you might have a form similar to the following (usually in your template file instead of directly in your views.py which we’ve done here for simplicity).

Click to Copy
from django.http import HttpResponse def home(request): first_name = request.GET.get("first_name") sort = request.GET.get("sort") text = f"""<h1>Searched for first name: {first_name} with sort option {sort}<h1> <form> <input name="first_name" type="text"> <input name="sort" type="text"> <input type="submit"> </form> """ return HttpResponse(text)

Now when you enter data into the two textboxes and press “Submit” on the page shown when you run your app, you’ll see the data appear in your URL and in the returned string.

Using POST instead of GET

When you submit using a GET request (the default for HTML forms), the form data is encoded into the URL. If you want to avoid this happening (e.g. for sensitive data that you don’t want tracked in your users’ browser history), you can use POST instead by using request.POST.get("first_name"). For example:

Click to Copy
from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt @csrf_exempt def home(request): first_name = request.POST.get("first_name") sort = request.POST.get("sort") text = f"""<h1>Searched for first name: {first_name} with sort option {sort}<h1> <form method="POST"> <input name="first_name" type="text"> <input name="sort" type="text"> <input type="submit"> </form> """ return HttpResponse(text)
  • Syntax.fmListen to the Syntax Podcast
  • Community SeriesIdentify, Trace, and Fix Endpoint Regression Issues
  • ResourcesBackend Error Monitoring 101
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark of Functional Software, Inc.