Home > Back-end >  Django dev server does not automatically reload after saving files
Django dev server does not automatically reload after saving files

Time:05-26

I tested Django 3 a few months ago, and it all worked well. The browser refreshed itself after I made a change to files (.html and .py).

Now I have an issue with a newly created Django project, the browser doesn't automatically reload after I saved a change that I made on my local machine.

OS: Windows 11
Editor: PyCharm / VS Code
Django 4.0.4
Python 3.10.4

Directory structure

project/
├── project/
│   ├── ...
│   ├── settings.py
│   ├── urls.py
│   └── ...
├── first_app/
│   ├── ...
│   ├── urls.py
│   ├── views.y
│   └── ...
├── templates/
│   └── first_app/
│       └── index_view.html
└── manage.py

Default settings.py file with

....
INSTALLED_APPS = [
   ...
   'first_app',
   ...
]

'DIRS': [BASE_DIR / 'templates']
....

project/urls.py

....
urlpatterns = [
   path('', include('first_app.urls')
]
....

first_app/views.py

....
class IndexView(generic.TemplateView):
   template_name = 'first_app/index_view.html'
...

first_app/urls.py

....
urlpatterns = [
   path('', views.IndexView.as_view(), name='index')
]
....

templates/first_app/index_view.html

....
<p>
   Test paragraph 1
</p>
....

I run the server locally with py manage.py runserver

When I change 'paragraph' to 'example' in index_view.html and save it, it is supposed to automatically refresh the browser and display Test example 1. But the browser doesn't reload.

I have created new Django projects several times and the results are still the same. Am I missing something?

I have tried the solutions on the following questions, but none of the solutions work for me.
https://stackoverflow.com/questions/68974685/why-isnt-the-django-dev-server-reloading/71618373\ Django server not reloading on file changes

Thanks.

Update:
py manage.py runserver output

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
May 25, 2022 - 21:40:08
Django version 4.0.4, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

CodePudding user response:

(Note: I'm capturing the summary of the discussion in the comments -- you found the actual problem in the end)

By default, Django is only able to reload the test server on code or template changes, it is not able to refresh the browser automatically.

If your browser was refreshing the page by itself for your previous projects, either it was triggered by a signal from your IDE, or by some javascript in your pages that was forcing a refresh.

It turns out you had the third-party package django-browser-reload installed in your previous projects, which injects a small script into your pages that refresh the page whenever the Django test server reloads. Adding that package to your new project will resolve the issue.

  • Related