Flask css 不更新 [英] Flask css not updating
问题描述
我在我的 mac 上使用 Flask(python 包),当我第一次编写我的 css 时它显示正常.但是,当我更新它并尝试检查它时,我只能看到第一个 css 样式.我试过重新启动终端,以及重新安装 Flask.有什么建议?谢谢.这是 HTML:
<头><title>标题</title><link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">头部><身体><标题><div class="header"></div><div class="logo"><center><img src="/static/img/p_logo.png" alt="pic"/></center>
</标题><div class="容器">{% 块内容 %}{% 结束块 %}
这里是 CSS:
* {font-family: "Times New Roman", Times, serif;}标题{背景色:#000000;宽度:100%;高度:7px;}
问题,如前所述,与浏览器缓存有关.
要解决这个问题,您可以向静态(css、js)链接添加一些动态变量.我更喜欢每个文件的最后修改时间戳.
/static/css/style.css?q=1280549780
这是一个片段:
http://flask.pocoo.org/snippets/40/
@app.context_processordef override_url_for():返回字典(url_for=dated_url_for)def dated_url_for(endpoint, **values):如果端点 == '静态':文件名 = values.get('文件名', 无)如果文件名:file_path = os.path.join(app.root_path,端点,文件名)values['q'] = int(os.stat(file_path).st_mtime)返回 url_for(endpoint, **values)
I am using Flask (the python Package) on my mac, when I first wrote my css it displayed ok. However when I updated it and tried to check it, I only see the first css styles. I have tried restarting the terminal, as well as reinstalling Flask. Any suggestions? Thanks. Heres the HTML:
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<header>
<div class="header"></div>
<div class="logo">
<center><img src="/static/img/p_logo.png" alt="pic"/></center>
</div>
</header>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
And heres the CSS:
* {
font-family: "Times New Roman", Times, serif;
}
header {
background-color: #000000;
width: 100%;
height: 7px;
}
Problem is, as already said, related to browser cache.
To solve that, you could add some dynamic variable to your static (css, js) links. I prefer last modified timestamp for each file.
/static/css/style.css?q=1280549780
Here is a snippet for that:
http://flask.pocoo.org/snippets/40/
@app.context_processor
def override_url_for():
return dict(url_for=dated_url_for)
def dated_url_for(endpoint, **values):
if endpoint == 'static':
filename = values.get('filename', None)
if filename:
file_path = os.path.join(app.root_path,
endpoint, filename)
values['q'] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
这篇关于Flask css 不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!