在项目目录下建一个static目录,下面可以建很多目录,如图
1在模板中分别放入相应文件加载网站图标
{# 加载网站的图标 #}
<link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon" />
通过url_for解析出路径显示出来
2加载图片、css、js
{% block page_content %}
<img src="{{ url_for('static', filename='img/meinv.jpg') }}">
<div class="test"></div>
{% endblock %}
{% block scripts %}
{{ super() }}
<script type="text/javascript" src="{{ url_for('static', filename='js/common.js') }}"></script>
{% endblock %}
{% block styles %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='css/common.css') }}" type="text/css">
{% endblock %}
同理,利用url_for解析出路径显示出来
最后还是提一下url_for的使用
在后台代码中使用url_for
#return redirect('/')
# 根据视图函数,反向构造对应的路由,传递的参数是视图函数名
#return redirect(url_for('response'))
# 可以拼接路由参数,多余的参数会以get方式拼接在url后面
#return redirect(url_for('hello', username='xiaoming', page=2))
# 当需要构造外部跳转的链接时,需要将_external设置为True以构造完整路由
return url_for('hello', username='xx', _external=True)
url_for('hello') ==> /hello/
url_for('hello', _external=True) ==> http://127.0.0.0:5000/hello/
很像django里面的反向解析