简体   繁体   English

我可以配置Django runserver在静态或非python文件更改时重新加载吗?

[英]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 默认情况下,Django的runserver命令会在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 是否可以将Django配置为将其文件监视扩展到其他目录或文件集 , such as JavaScript or CSS files being served statically (during development) ,例如静态提供的JavaScript或CSS文件(在开发期间) ?

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. 这在这种情况下很有用:Django应用程序在启动时读取一组静态文本文件,我希望服务器在更改时重新读取它们,而不必添加此特定功能 - 只需重新启动即可。

Do I need to start meddling with (or extending) django/utils/autoreload.py ? 我是否需要开始干预(或扩展) 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. 正如您对问题的评论所说,Django总是在每个请求中从文件系统中提取文件,因此它们不会被缓存。

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. 但是,如果浏览器发送If-Modified-Since标头,则会检查(在django.views.static )文件的mtime,这就是为什么您可能会看到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 Ctrl + Shift + rCtrl + f5

If your on mac use CMD button instead of ctrl 如果您在Mac上使用CMD按钮而不是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. 而是在开发时运行collectstatic ,它将最近编辑的静态文件(如javascript)从一个目录复制到服务器使用的目录。

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. 然后您的服务器将看到文件中的所有更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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