Django – Adding Hot Reload (with sample)

Hello Coders! Implementing hot reload for templates and static files in Django can greatly improve your development workflow. You can take a few different approaches to achieve hot reload in Django, and this article presents the most simple ones. To make this article more useful, in the end, an open-source sample is provided. Thanks for reading!


1# – Watchgod Utility

One popular method is to use the django-extensions package along with watchgod for auto-reloading.

$ pip install django-extensions
Enter fullscreen mode
Exit fullscreen mode

Then, you can add django_extensions to your INSTALLED_APPS in your Django settings file (settings.py).

For hot reloading of templates, you can use the runserver_plus command provided by django-extensions, which automatically reloads templates when they change.

$ python manage.py runserver_plus
Enter fullscreen mode
Exit fullscreen mode

For static files, you can use watchmedo (a part of watchgod) to monitor changes and trigger reloads:

$ watchmedo shell-command --patterns="*.css;*.js;*.html" --command="python manage.py collectstatic --noinput" --recursive static
Enter fullscreen mode
Exit fullscreen mode

This command watches for changes in your static files directory and triggers collectstatic whenever a change is detected. Combined with Django’s built-in development server, this provides a pretty seamless hot reload experience for both templates and static files.


2# – Django-Browser-Reload

This open-source library can be integrated with any Django project with only a few lines of code as stated on the official PyPi page:

Install the library using PIP or any other package manager

$ pip install django-browser-reload
Enter fullscreen mode
Exit fullscreen mode

Make sure you have “django.contrib.staticfiles” in your INSTALLED_APPS section. Besides this, a new application should be added:

INSTALLED_APPS = [ ..., "django_browser_reload", # <-- NEW
 ...,
]
Enter fullscreen mode
Exit fullscreen mode

The next step is to update the project routing rules to include the library URLs:

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

Updating the middleware is the last configuration step required for this integration.

MIDDLEWARE = [ # ...
 "django_browser_reload.middleware.BrowserReloadMiddleware", # ...
]
Enter fullscreen mode
Exit fullscreen mode

The middleware automatically inserts the required script tag on HTML responses before the BODY tag when DEBUG is True.

If all the above steps were followed, our Django project should support the HOT reload feature, and all the changes we made to the templates and static files are automatically synced with the browser.


For those in a rush for a sample, Rocket Django, an open-source Tailwind-based starter provides already the integration for Django-Browser-Reload Library:

Rocket Django - Open-Source Starter with Hot Reload feature


Testing the HOT Reload

Once we download the sources of Rocket Django from the official product page, we need to follow the usual set-up steps for every Django project:

  • Open project using VsCode or any other code editor
  • Install dependencies
  • Migrate database
  • Install Tailwind dependencies and watch the CSS files using the command:
$ npx tailwindcss -i ./static/assets/style.css -o ./static/dist/css/output.css --watch
Enter fullscreen mode
Exit fullscreen mode
  • Start the Django Development server
$ python manage.py runserver
Enter fullscreen mode
Exit fullscreen mode

At this point, all changes made on the Python files, templates [ HTML ], and assets [ CSS, JS ] are automatically reflected in the browser:

<img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdzovh5k23hzfxpbn9at0.png" alt="Rocket Django – Sample page content updated with HOT Reload.
” loading=”lazy” width=”800″ height=”322″>


Conclusion

Using the right tooling (hot reload for instance) combined with a good development strategy might help us be more productive and deliver the projects with better quality.


Thanks for reading! For more resources, feel free to access:


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Privacy, cookie & data policy.