如果不清除缓存,为什么我的Flask应用无法渲染CSS? [英] Why does my Flask app not render CSS if I don't clear my cache?

查看:89
本文介绍了如果不清除缓存,为什么我的Flask应用无法渲染CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Flask,并将我渲染的html链接到CSS样式表.我注意到,每当更新CSS代码时,都必须清除缓存以使其在网页上更新.我该如何解决?

I've been using Flask and have linked the html I'm rendering to a css stylesheet. I've noticed that whenever I update my CSS code, I have to clear the cache in order to get it to update on the webpage. How can I fix this?

推荐答案

我假设您正在加载CSS文件,例如:

I assume you are loading your CSS files with something like:

{{ url_for('static', filename='some/file.css') }}

要使其在开发中立即刷新,应将以下配置变量设置为 -1 :

For this to refresh immediately in development, you should set the following config var to -1:

app.config['SEND_FILE_MAX_AGE_DEFAULT'] = -1

正如@Matvei所说,此问题更多与您的浏览器有关.要对此进行可视化,请打开开发工具,转到 Network 标签并突出显示特定的CSS文件.然后在右侧的部分中的 Headers -> Response Headers "下找到以下行:

As @Matvei states, this issue is more to do with your browser. To visualise this, open the dev tools, go to the Network tab and highlight the specific CSS file. Then in the section on the right look for the following line under Headers -> Response Headers:

Cache-Control: public, max-age=-1

如果正确应用了设置,则应显示 -1 .如果显示任何其他内容,则需要刷新该特定文件,直到它显示 -1 为止,可能必须清除缓存.这是因为浏览器根据 Cache-Control 标头来选择是否重新加载文件.

If the setting has been applied correctly this should show -1. If it shows anything else you need to refresh that specific file, until it does show -1, possibly having to clear your cache. This is because the browser makes the choice of whether to reload the file based on the Cache-Control header.

关于此的类似的答案,其中包含屏幕截图和指向文档的链接.

See my similar answer on this with screencaps and links to the docs.

这篇关于如果不清除缓存,为什么我的Flask应用无法渲染CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆