Django HttpRequest
last modified November 13, 2026 Python 3.15
Django HttpRequest tutorial shows how to examine the HttpRequest object in a Django view. The examples use Django 6.1 and Python 3.15, and the environment is managed with uv.
Django
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It is maintained by the Django Software Foundation. If you are new to the framework, start with the Django introduction article.
Django HttpRequest
Django uses request and response objects to pass state through the system.
When a page is requested, Django creates an HttpRequest object
that contains metadata about the request: the method, the path, the headers,
the query parameters, the cookies, and the uploaded data. Django then selects
the matching view and passes the HttpRequest as the first argument
to the view function. Each view must return an HttpResponse
object.
The most commonly used attributes are the following.
method- the HTTP method, such asGETorPOSTpath- the requested path without the domain and query stringscheme-httporhttpsGETandPOST- dictionary-like objects with the query and form parametersheaders- a case-insensitive dictionary of the HTTP headersMETA- a dictionary of all available metadata, such asREMOTE_ADDRCOOKIESandFILES- the cookies and the uploaded files
Creating the project
In the following example, we create a Django application that examines the
data sent in an HttpRequest object.
$ mkdir httprequest $ cd httprequest $ uv init --bare $ uv add django $ uv run django-admin startproject config .
We create a project directory, initialize it with uv, and install Django into a
project-local virtual environment. The last command creates the Django project
inside the current directory; the dot at the end prevents Django from
creating an extra config directory level. All commands are run
with uv run, so there is no need to activate the environment.
Note: The Django way is to put functionality into apps, which
are created with uv run python manage.py startapp. In this
tutorial, we do not use an app to keep the example simple. We focus on
examining the HttpRequest.
Project structure
We add a views.py module to the config package.
The project then has the following structure; the hidden .venv
directory is not shown.
. ├── config │ ├── asgi.py │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ ├── views.py │ └── wsgi.py ├── manage.py ├── pyproject.toml └── uv.lock
URL configuration
The URL configuration maps request paths to view functions. Django reads the urlpatterns list from top to bottom and calls the first view whose path matches.
from django.contrib import admin
from django.urls import path
from .views import data
urlpatterns = [
path('admin/', admin.site.urls),
path('data/', data, name='data'),
]
We add a new data/ path; it calls the data function
from the views.py module. The module docstring generated by
Django is omitted from the listing.
The view
The view is a plain Python function. It receives the HttpRequest and returns an HttpResponse.
from django.http import HttpResponse
from django.utils.html import escape
def data(request):
path = request.path
scheme = request.scheme
method = request.method
address = request.META['REMOTE_ADDR']
user_agent = request.headers.get('User-Agent', 'unknown')
name = request.GET.get('name', 'guest')
msg = f'''<!DOCTYPE html>
<html>
<body>
Path: {escape(path)}<br>
Scheme: {scheme}<br>
Method: {method}<br>
Address: {address}<br>
User agent: {escape(user_agent)}<br>
Name: {escape(name)}<br>
</body>
</html>
'''
return HttpResponse(msg)
Inside data, we get the request's path, scheme, method, remote
address, user agent, and the optional name query parameter. The
headers mapping is case-insensitive, and get() with
a default avoids a KeyError when the client sends no
User-Agent header. The REMOTE_ADDR key is always set by
the development server, so we can index META directly.
The user agent and the query parameter come from the client, so we pass them
through escape() before placing them in HTML. Without it, a request
such as ?name=<script>... would inject markup into the page
(cross-site scripting).
Note: For this simple example, we have built the HTML string
manually. An HTML response is normally created with Django's template
language, which escapes variables automatically. The default content type of
HttpResponse is text/html; charset=utf-8.
Running the application
We start the development server.
$ uv run python manage.py runserver
In another terminal, we use the curl tool to request the
/data/ path with a query parameter. A browser at
http://127.0.0.1:8000/data/?name=Peter shows the same data. If we
omit the parameter, the name is guest.
$ curl 'http://127.0.0.1:8000/data/?name=Peter' <!DOCTYPE html> <html> <body> Path: /data/<br> Scheme: http<br> Method: GET<br> Address: 127.0.0.1<br> User agent: curl/7.81.0<br> Name: Peter<br> </body> </html>
Source
Django documentation: HttpRequest objects
Django documentation: Writing views
In this article we have examined the HttpRequest object in a Django view. The other side of the exchange is described in Django HttpResponse; the first steps with the framework are covered in Django introduction.
Author
List all Django tutorials.