【问题标题】:Django, forcing reload of css/js, and collectstaticDjango,强制重新加载 css/js,并收集静态
【发布时间】:2019-09-03 16:19:50
【问题描述】:

假设我在我的开发机器上对我的 css 文件做了一些更新(我的浏览器设置为忽略缓存,并不总是立即看到更新)。

this question等地方,我看到了给链接加版本号的做法:

<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 时会发生什么?它将从哪里获取文件以覆盖这些文件?

【问题讨论】:

    标签: css django static-files


    【解决方案1】:

    推荐的方法是使用 django-compressor (usage) 之类的东西将您的 CSS 处理成单个文件,然后将哈希值烘焙到文件名或目录中。这样你的资产就会变成/mystyles.beb23fe.css

    【讨论】:

      【解决方案2】:

      最终很容易解决的是更改版本标签的格式:

      <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">

      在开发和生产方面都很好。

      【讨论】:

        猜你喜欢
        • 2011-12-01
        • 1970-01-01
        • 2016-09-29
        • 2018-02-03
        • 2015-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多