Skip to content

Instantly share code, notes, and snippets.

@Roger8
Last active December 17, 2018 03:07
Show Gist options
  • Save Roger8/e7320b021a9d245c3ee3caf8cdd88634 to your computer and use it in GitHub Desktop.
Save Roger8/e7320b021a9d245c3ee3caf8cdd88634 to your computer and use it in GitHub Desktop.
flask 使用笔记

在使用Flask静态文件的时候,每次更新,发现CSS或是Js或者其他的文件不会更新

解决方法:(1) 清理浏览器缓存 (2) 设置Flask框架中app的config默认参数
  from datetime import timedelta
  app = Flask(__name__)
  app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(seconds=1) # 我们配置缓存最大时间

参考

Flask找不到static文件夹下静态文件

由于本地ip映射的实际域名并非www.test.com而是www.test.com/sevr1, 导致本地static文件夹 下资源在html模板文件下的地址无法获取,通过添加函数route解决, html文件中静态资源的获取需要使用url_for 函数获取, 直接使用字符串也获取不到#"static/img/s27653416.jpg"#
#  main.py
app = Flask()
   @app.route('/sevr1/static/<filename>')
   def mystatic(filename):
       return send_from_directory('./static',filename,as_attachment=True) 

#index.html 模板文件
<html>
<script type="text/javascript" src="{{ url_for('static', filename='js/fun.js') }}"></script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment