why isn't the Django dev server reloading?

Viewed 1134

I've used django dev servers for years. I start them with

python manage.py runserver

and when I change any of the python code, they restart nicely.

Now I've inherited someone else's project for maintenance, and for whatever reason, I change the Python code and the server does not restart. I can't seem to find any kind of configuration option that would cause this.

I don't have the --noreload flag set, here's my proof:

$ ps -ef | grep runserver
chris     290898  250765  1 11:55 pts/6    00:00:00 python manage.py runserver
chris     290914  290898  5 11:55 pts/6    00:00:01 /home/chris/.virtualenvs/my-webapp-4cLYw-9X/bin/python manage.py runserver

Has anyone run into this before?

  • Python 3.9.5
  • Django 3.2.6
  • using pipenv to manage my virtual environment

UPDATE

More info...equally as odd

the project is set up like this

src/manage.py
src/myproject (contains settings.py, urls.py, etc)
src/apps/app_one (contains models.py, tests.py, etc)
src/apps/app_two (contains models.py, tests.py, etc)

If I edit any file under src/myproject, then the server restarts as expected. But if I edit files under app_one or app_two, then the server does not restart.

UPDATE #2

Resurrecting this before it drives me crazy (it may be too late).

I added django.utils.autoreload': {'level': 'DEBUG'}, to settings.LOGGING, and now I can see that Django is watching my files, because I can see this in the logs:

[15/Feb/2022 10:58:14] django.utils.autoreload - DEBUG [tick:392] File /home/chris/OBFUSCATED/src/apps/account/models.py previous mtime: 1644940666.8954673, current mtime: 1644940693.9396086

[15/Feb/2022 10:58:14] django.utils.autoreload - DEBUG [notify_file_changed:366] /home/chris/OBFUSCATED/src/apps/account/models.py notified as changed. Signal results: [(<function template_changed at 0x7efd4b77b550>, True), (<function translation_file_changed at 0x7efd48257f70>, None)].

And yet, the dev server does not restart. Hoping this makes more sense to someone than it does to me.

3 Answers

UPDATE

This is fixed in Django 3.2.13 and Django 4.0.4

Original answer

I have a similar issue, and it appears to be a bug. I tracked it down to Django 3.2.4 (3.2.3 works as expected).

See the Release notes for Django 3.2.4. This is the bugfix that breaks it for me.

It's still broken as of 4.0.3.

UPDATE

In my case this is due to having [''] in DIRS, use filter if you are getting the value from the enviroment:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": filter(None, os.getenv("TEMPLATES_DIRS", "").split(",")),
...

I've opened a bug and a PR

Auto-reloading is a feature of Django runserver and is only used in development mode.

read this in Django docs:

If you’re using Linux or MacOS and install both pywatchman and the Watchman service, kernel signals will be used to autoreload the server (rather than polling file modification timestamps each second). This offers better performance on large projects, reduced response time after code changes, more robust change detection, and a reduction in power usage. Django supports pywatchman 1.2.0 and higher.

But in production mode servers run by wsgi.py file and managed by python servers like gunicorn, uwsgi, ...

This feature is not preferred in production mode because you may need to change multiple files and you save them one by one and auto-reload may cause error pages in production.

Have had the same issue. I finally identified the issue. In my settings.TEMPLATES, the DIRS was [BASE_DIR], while it should be [BASE_DIR / 'templates']. Once I fixed this, autoreload worked again.

Related