'Can I configure Django runserver to reload when static or non-python files are changed?

By default, Django's runserver command auto reloads the server when python or template files are changed.

Is it possible to configure Django to extend its file monitoring for this purpose to other directories or sets of files, such as JavaScript or CSS files being served statically (during development)?

This would be useful in this scenario: the Django app reads a set of static text files on startup and I would like the server to re-read them when they change, without having to add this specific feature - simply restarting would be fine.

Do I need to start meddling with (or extending) django/utils/autoreload.py ?



Solution 1:[1]

The static files are automatically served from disk, so there is no need to reload the dev server.

But your browser has it's own cache, and is keeping some of your static files in it... To reload it use this shortcut :

Ctrl + Shift + r OR Ctrl + f5

If your on mac use CMD button instead of ctrl

Solution 2:[2]

There is no need to reload server, but sometimes there is need to copy static files to be visible for the server.

Instead running collectstatic while developing, which copy recently edited static files (like javascript) from one directory to the directory, used by server.

here is a trick:

  • link source directory to behalf of the target (will "override" target directory)
  • or run in loop:

python manage.py collectstatic --noinput

then your server will see all changes in files.

Solution 3:[3]

The file will by default be read from disk on every request, so there is no need to restart anything.

There is a caching template loader, but it is disabled by default. See the documentation for more info.

Solution 4:[4]

As the comments on your question say, Django always pulls the file from the filesystem on every request, so they are not cached.

However, there is a check (indjango.views.static) for the mtime of the file if the browser sends an If-Modified-Since header which is why you may be seeing 304 Not Modified.

Regardless, would simply disabling browser caching meet your needs?

Solution 5:[5]

The answer to this is YES, all you need to do is touch your settings file which will trigger a runserver reload. If all you need to do is source new static files, you don't need this, but if you need to trigger a reload for another reason, it is possible with a simple touch.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sławomir Lenart
Solution 3 abhi
Solution 4 Steve Jalim
Solution 5 boatcoder