Django development server restarts when changing template (HTML) files

Viewed 152

I started a new project in Django 3.2. Unlike what I am used to, on my development machine the runserver restarts automatically when I change template files (HTML files). I am not used to this happening - normally these would always be loaded from disk and changes did not require a restart. A restart is fast but not instantaneous and I rather have the old behavior. I checked the changelog and the runserver documentation but I am not sure if this really is a recent change, or a setting, or if I am overlooking something.

Anyone any idea? Below the output of the server in my docker container where you can see that a change to an HTML file triggers the restart...

web_1      | /src/templates/_nav.html changed, reloading.
web_1      | Watching for file changes with StatReloader
web_1      | Performing system checks...
web_1      | 
web_1      | System check identified no issues (0 silenced).
web_1      | April 27, 2021 - 14:16:17
web_1      | Django version 3.2, using settings 'demo.settings'
web_1      | Starting development server at http://0.0.0.0:8000/
web_1      | Quit the server with CONTROL-C.
1 Answers

This was a bug in Django having a ticket #32744 and it is fixed in version 3.2.4 (release notes). The simplest solution would hence be to upgrade Django to a newer version:

python -m pip install -U Django

Or if you cannot do that for some reason then you need to fix the indirect cause of the problem, which is that newer Django projects use pathlib.Path whereas older projects used strings to indicate the template directory, etc. hence a solution would be to upgrade / update your settings to use pathlib.Path. In general updating the below settings should help you (you might need to change others too where you use BASE_DIR):

from pathlib import Path


BASE_DIR = Path(__file__).resolve().parent.parent


# showing only relevant parts of the TEMPLATES setting
TEMPLATES = [
    {
        'DIRS': [BASE_DIR / 'templates'] # I don't show the other keys, but don't remove them!
    },
]
Related