首页 > 解决方案 > Django,强制重新加载 css/js,并收集静态

问题描述

假设我已经对我的开发机器上的 css 文件进行了大量更新(我的浏览器设置为忽略缓存,并不总是立即看到更新)。

这个问题和其他地方,我看到了在链接中添加版本号的方法:

<link type="text/css" href={% static "/path/mystyles.css?version=2" %} rel="stylesheet">,

这很好,但是在开发机器上,我在控制台中收到错误,并且找不到文件:

GET http://127.0.0.1:8000/path/mystyles.css%3Fversion%3D2 net::ERR_ABORTED 404 (Not Found)

我读过这可能collectstatic会在开发方面修复它,但有一个看起来不祥的警告:

You have requested to collect static files at the destination
location as specified in your settings:

    /patttthhhhh/static

This will overwrite existing files!
Are you sure you want to do this?

当我跑步时会发生什么collectstatic?它将从哪里获取文件以覆盖这些文件?

标签: cssdjangostatic-files

解决方案


最终轻松工作的是更改版本标签的格式:

<link type="text/css" href="{% static '/path/mystyle.css' %}?version=2" rel="stylesheet">

代替:

<link type="text/css" href={% static "/path/mystyles.css?version=2" %} rel="stylesheet">

在开发和生产中都很好。


推荐阅读