Package: django-browser-reload
Automatically reload your browser in development.Work smarter and fasterwith my bookBoost Your Django DXwhich covers django-browser-reload and many other tools.
I wrote django-browser-reload whilst working on the book!RequirementsPython 3.8 to 3.12 supported.Django 3.2 to 5.0 supported.WSGI supported on all Django versions. ASGI supported on Django 4.2+.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).InstallationInstall withpip:python-mpipinstalldjango-browser-reloadEnsure you have"django.contrib.staticfiles"in yourINSTALLED_APPS.Add django-browser-reload to yourINSTALLED_APPS:INSTALLED_APPS=[...,"django_browser_reload",...,]Include the app URLs in your root URLconf:fromdjango.urlsimportinclude,pathurlpatterns=[...,path("__reload__/",include("django_browser_reload.urls")),]You can use another prefix if required.Add the middleware:MIDDLEWARE=[# ..."django_browser_reload.middleware.BrowserReloadMiddleware",# ...]The middleware should be listed after any others that encode the response, such as Django’sGZipMiddleware.The middleware automatically inserts the required script tag on HTML responses before</body>whenDEBUGisTrue.
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! 📯For faster and more efficient reloading, also set upDjango’s built-in Watchman support.What It DoesWhenDEBUGisTrue, the template tag includes a small script.
This script connects back to the development server and will automatically reload when static assets or templates are modified, or afterrunserverrestarts.
The reload only happens in the most recently opened tab.Example ProjectSee theexample projectin theexample/directory of the GitHub repository.
Start it up and modify its files to see the reloading in action.Template TagIf the middleware doesn’t work for you, you can also use a template tag to insert the script on relevant pages.
The template tag has both Django templates and Jinja versions, and only outputs the script tag whenDEBUGisTrue.ForDjango 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>To add django-browser-reload to Django’s admin, do so in a template calledadmin/base_site.html:{% extends "admin/base_site.html" %}

{% load django_browser_reload %}

{% block extrahead %}
    {{ block.super }}
    {% django_browser_reload_script %}
{% endblock %}This follows Django’s documentation onextending an overridden template.ForJinja Templates, you need to perform two steps.
First, load the tag function into the globals of yourcustom environment:# myproject/jinja2.pyfromjinja2importEnvironmentfromdjango_browser_reload.jinjaimportdjango_browser_reload_scriptdefenvironment(**options):env=Environment(**options)env.globals.update({# ..."django_browser_reload_script":django_browser_reload_script,})returnenvSecond, render the tag in your base template.
It can go anywhere, but it’s best just before</body>:...
    {{ django_browser_reload_script() }}</body></html>Ta-da!How It WorksHere’s a diagram:Browser

                             Tab 1    Tab 2     Tab N
                           listener  listener  listener
                                \       |       /
  Django                         \      |      /
                                  \     |     /
Events View --------------------> Shared workerThe template tag includes a listener script on each page.
This listener script starts or connects to aSharedWorker, running a worker script.
The worker script then connects to the events view in Django, using anEventSourceto receive server-sent events.This event source usesStreamingHttpResponseto send events to the worker.
The view continues streaming events indefinitely, until disconnected.
(This requires a thread and will not work if you userunserver’s--nothreadingoption.)On a relevant event, the worker will reload the most recently connected tab.
(It avoids reloadingalltabs 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 - ifanytemplate 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’sEventSourcereconnects 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.