简体   繁体   中英

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 ?

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.

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 (in django.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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM