Django HttpResponse
last modified November 13, 2026 Python 3.15
Django HttpResponse tutorial shows how to send string data with HttpResponse in Django. 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 HttpResponse
HttpResponse is a response class that holds string or bytes data.
While HttpRequest is created by Django, HttpResponse
is created by the programmer; every view must return one. It accepts the
content, an optional content_type, and an optional
status code.
HttpResponse has subclasses including JsonResponse,
StreamingHttpResponse, and FileResponse. There are
also shortcuts for common status codes, such as
HttpResponseNotFound and HttpResponseRedirect.
Creating the project
In the following example, we create a Django application that sends text data to the client. We send the current date and time.
$ mkdir httpresponse $ cd httpresponse $ 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 sending
HttpResponse.
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 now
urlpatterns = [
path('admin/', admin.site.urls),
path('now/', now, name='now'),
]
We add a new path now/; it calls the now 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 datetime import datetime
from django.http import HttpResponse
def now(request):
msg = f'Today is {datetime.now():%Y-%m-%d %H:%M:%S}'
return HttpResponse(msg, content_type='text/plain; charset=utf-8')
Inside now, we determine the current date and time and format it
with an f-string format specification. We build a message and send it to the
client in an HttpResponse. The content type is set to plain text.
When we pass content_type explicitly, Django uses it verbatim, so
we include the charset ourselves; with the default content type Django adds
charset=utf-8 automatically.
Running the application
We start the development server.
$ uv run python manage.py runserver
In another terminal, we use the curl tool to make the GET request
to http://127.0.0.1:8000/now/. The -i option prints
the response headers, where we can see the content type.
$ curl -i http://127.0.0.1:8000/now/ HTTP/1.1 200 OK Date: Fri, 13 Nov 2026 17:54:03 GMT Server: WSGIServer CPython/3.15.2 Content-Type: text/plain; charset=utf-8 X-Frame-Options: DENY Content-Length: 28 X-Content-Type-Options: nosniff Referrer-Policy: same-origin Cross-Origin-Opener-Policy: same-origin Today is 2026-11-13 17:54:03
Source
Django documentation: HttpResponse objects
In this article we have sent text data from a Django view with HttpResponse. Specialized responses are described in Django JsonResponse and Django FileResponse; the incoming side is covered in Django HttpRequest.
Author
List all Django tutorials.