Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why isn't the Django dev server reloading?

Tags:

django

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.

like image 835
Chris Curvey Avatar asked Jul 21 '26 09:07

Chris Curvey


2 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

like image 121
Manel Clos Avatar answered Jul 25 '26 03:07

Manel Clos


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.

like image 34
Ikari Avatar answered Jul 25 '26 05:07

Ikari