Skip to main content
https://img.shields.io/github/actions/workflow/status/adamchainz/django-browser-reload/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/django-browser-reload.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Automatically refresh your browser on changes to Python code, templates, or static files.


Work smarter and faster with my book Boost Your Django DX which covers django-browser-reload and many other tools. I wrote django-browser-reload whilst working on the book!


Requirements

Python 3.9 to 3.14 supported.

Django 4.2 to 6.0 supported.

Both WSGI and ASGI are supported.

Your browser needs to support:

  • EventSource - universally available.

  • SharedWorker - available on Chrome, Edge, Firefox, and Opera for a long time. Available on Safari since version 16 (2022-09-12).

Installation

  1. Install with pip:

    python -m pip install django-browser-reload
  2. Ensure you have "django.contrib.staticfiles" in your INSTALLED_APPS.

  3. Add django-browser-reload to your INSTALLED_APPS:

    INSTALLED_APPS = [
        ...,
        "django_browser_reload",
        ...,
    ]
  4. Include the app URLs in your root URLconf:

    from django.urls import include, path
    
    urlpatterns = [
        ...,
        path("__reload__/", include("django_browser_reload.urls")),
    ]

    You can use another prefix if required.

  5. Add the middleware:

    MIDDLEWARE = [
        # ...
        "django_browser_reload.middleware.BrowserReloadMiddleware",
        # ...
    ]

    The middleware should be listed after any others that encode the response, such as Django’s GZipMiddleware.

    The middleware automatically inserts the required script tag on HTML responses before </body> when DEBUG is True. It does so to every HTML response, meaning it will be included on Django’s debug pages, admin pages, etc. If you want more control, you can instead insert the script tag in your templates—see below.

All done! 📯

Try installing django-watchfiles as well, for faster and more efficient reloading.

Usage

Once set up as above, just run runserver with the DEBUG setting set to True, and open your site in a browser. When you modify Python code, templates, or static assets, the page will automatically reload. Welcome to much faster iteration times!

If you open multiple tabs, only the most recently used tab will reload.

django-browser works by adding a script tag into HTML responses, just before </body>. This script connects back to the development server and receives events that tell it when to reload. These events are triggered through server restarts and runserver’s autoreload system. See below for a more detailed explanation, under “How It Works”.

On Django 6.0+ with ContentSecurityPolicyMiddleware, the <script> tag will include the Content Security Policy (CSP) nonce.

Template tag

You can also use a template tag to insert the script on relevant pages, instead of using the middleware. This may be useful if you want to restrict reloading to certain pages, or if the middleware doesn’t work for your page structure. The template tag has both Django templates and Jinja versions, and only outputs the script tag when DEBUG is True.

For Django Templates, load the tag and use it in your base template. The tag can go anywhere, but it’s best just before </body>:

{% load django_browser_reload %}

...

    {% django_browser_reload_script %}
  </body>
</html>

On Django 6.0+, the <script> tag will include the Content Security Policy (CSP) nonce, if it’s present in the context.

To add the template tag within Django’s admin, do so in a template called admin/base_site.html, per Django’s documentation on extending an overridden template. The template should look like:

{% extends "admin/base_site.html" %}

{% load django_browser_reload %}

{% block extrahead %}
    {{ block.super }}
    {% django_browser_reload_script %}
{% endblock %}

For Jinja Templates, you need to perform two steps. First, load the tag function into the globals of your custom environment:

# myproject/jinja2.py
from jinja2 import Environment
from django_browser_reload.jinja import django_browser_reload_script


def environment(**options):
    env = Environment(**options)
    env.globals.update(
        {
            # ...
            "django_browser_reload_script": django_browser_reload_script,
        }
    )
    return env

Second, render the tag in your base template. It can go anywhere, but it’s best just before </body>:

...
    {{ django_browser_reload_script() }}
  </body>
</html>

To use a CSP nonce, pass it to the function as nonce:

{{ django_browser_reload_script(nonce=csp_nonce) }}

Example project

To demonstrate and test django-browser-reload on all kinds of assets, there is an example project included in the repository. Open the example/ directory, follow the instructions in its README, and try it out.

How it works

Here’s a diagram:

                                     Browser

                             Tab 1    Tab 2     Tab N
                           listener  listener  listener
                                \       |       /
  Django                         \      |      /
                                  \     |     /
Events View --------------------> Shared worker

The middleware (or template tag) includes a listener script on each page. This listener script starts or connects to a SharedWorker, running a worker script. The worker script then connects to the events view in Django, using an EventSource to receive server-sent events.

This event source uses StreamingHttpResponse to send events to the worker. The view continues streaming events indefinitely, until disconnected. (This requires a thread and will not work if you use runserver’s --nothreading option.)

On a relevant event, the worker will reload the most recently connected tab. (It avoids reloading all tabs since that could be expensive.)

To reload when a template changes, django-browser-reload piggybacks on Django’s autoreloading infrastructure. An internal Django signal indicates when a template file has changed. The events view receives this signal and sends an event to the worker, which triggers a reload. There is no smart filtering - if any template file changes, the view is reloaded.

To reload when the server restarts, django-browser-reload uses a version ID. This ID is randomly generated when the view module is imported, so it will be different every time the server starts. When the server restarts, the worker’s EventSource reconnects with minimal delay. On connection, the events view sends the version ID, which the worker sees as different, so it triggers a reload.

The events view also sends the version ID every second to keep the connection alive.

Metadata

Release files for django-browser-reload 1.21.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-browser-reload 1.21.0
File Size Uploaded
django_browser_reload-1.21.0.tar.gz 15.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-browser-reload 1.21.0
File Interpreter ABI Platform
django_browser_reload-1.21.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.7 kB

Release files / django_browser_reload-1.21.0.tar.gz

Download URL django_browser_reload-1.21.0.tar.gz
Size 15.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3335ad3d107eb657f623d1a8e680dfbcab8a83ae1f94df1895e069dddf5604ba
BLAKE2b-256 checksum
How to use checksums
79efab407c1a2f14e13c75a6419a2d124954aa0364f62580ad95a3d31dc6c73d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2025.

Transparency log

Release files / django_browser_reload-1.21.0-py3-none-any.whl

Download URL django_browser_reload-1.21.0-py3-none-any.whl
Size 12.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0b2a86ab460774fa9bb142a121c70e75a72f18109f51a4f6de409cd633d3a70d
BLAKE2b-256 checksum
How to use checksums
46611b4a8c589652859995bcab87682286443eb9fdf2d7fd584975b9ffc1db33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 22, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.21.0 This release

2 release files

1.20.0

2 release files

1.17.0

2 release files

1.16.0

2 release files

1.15.0

2 release files

1.14.0

2 release files

1.13.0

2 release files

1.12.1

2 release files

1.12.0

2 release files

1.11.0

2 release files

1.10.0

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page