Page Auto Reload in Django

Dec 10, 2023    |   

To set up automatic page reloads in Django when there are changes in your server code, templates, content, or classes, follow these steps:

1. Install django-browser-reload:

python -m pip install django-browser-reload

2. Add "django_browser_reload" to the INSTALLED_APPS

 Add "django_browser_reload" to the INSTALLED_APPS list in your Django project's settings.

INSTALLED_APPS = [
    ...,
    "django_browser_reload",
    ...,
]

 

3. Include the django-browser-reload app's URLs:

Include the django-browser-reload app's URLs in your project's URL configuration in your project's URL configuration.

from django.urls import include, path

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

4. Add to the MIDDLEWARE list

Add "django_browser_reload.middleware.BrowserReloadMiddleware" to the MIDDLEWARE list in your project's settings, ensuring it comes after any middleware that encodes the response.

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

Finished and enjoy automatic page reloading now.