Gareth D.
—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 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:
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:
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”.
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).
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.
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:
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)
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.