Django Request Object
Learn how to read and manage the information provided by an HTTP request in Django
This page explains the request
object provided by Django as the default argument in views. For those unfamiliar with Django, this web framework is written in Python by experienced developers using a batteries-included concept. For more information regarding this amazing web framework please access:
How to install Django - simple, tested steps to install Django
Django for beginners - a comprehensive tutorial that covers the basics
Let's create a Django project and code our first view where the request
object is exposed:
Create and activate a virtual environment
For Windows-based systems, the syntax is slightly different:
Install Django - using PIP
The above command will install the latest stable version of Django.
Build a Django Project
A new project can be generated with ease in Django by using django-admin that provides a collection of settings for the database, Django, and security layer.
Create the project folder
Inside the directory, we will generate the core of our project using django-admin tool :
Note: Take into account that .
at the end of the command.
Create the database and the app tables
Start the application
At this point we should see the default Django page in the browser:
Create New Application
In the previous section, we've generated the core of the project that handles the configuration and now we will create the first Django application to serve a simple page to the users.
Update settings to include the new app -
config/settings.py
Update routing -
config/urls.py
Code out first route and get access to the
request
object
Request Object Properties
The request
object in Django comes with a few interesting properties:
Item / Helper
Sample Value
Short Information
REMOTE_ADDR
12.245.68.71
IP of the client
HTTP_USER_AGENT
Mozilla/5.0
User Browser Information
request.path
/admin/
The URL without domain
request.is_secure()
True / False
True if the request was made over HTTPS
request.method
POST / GET
The Request Method
How to check the request type in view.
A widely user code chunk is when we test the type of the request and manage the submitted data on POST
(when we send information to the server, login action for instance):
For newcomers, GET
is used to pull information from the server without altering anything. Search is a good example of a GET
request when we try to locate information on Google or Wikipedia.
POST
is used to update the information on the server like change the title for a book or register a new item in our service.
Another difference between GET
and POST
is the location of the submitted information.
GET submits data in URL:
http://myservice.com?search=all_books
POST submits data in the request body (not visible in the URL)
List all request headers
This subsection contains a code sample that prints all headers and request objects provided by Django:
Here is the script output executed using a local development server:
Read Request Variables
Django provides dictionaries for GET
and POST
requests populated with all variables sent by the client. We can access the variables by key
or using get()
helper for both cases.
Read GET variables
For POST, the process is identical.
Thanks for reading! For more topics, feel free to contact Appseed.
Resources
Read more about Django (official docs)
Start fast a new project using development-ready Django Starters
Last updated
Was this helpful?