flask为网站添加favicon

设置favicon可以在html页面,也可以通过route,因为打开网站的时候,会自动寻找/favicon

通过html设置

如果是普通的html 只要在head之间加入

<link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.ico') }}" type="image/x-icon">

即可。
如果你使用了jinja2模板,则需在base中加一个{% block head %}来保证语句是在head之间的。否则无效。

{% block head %}
<link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.ico') }}" type="image/x-icon">
{% endblock %}

这样即可。
然后如果使用了bootstrap,就更麻烦了。因为flask-bootstrap的源码是这样的。。。

{% block doc -%}
<!DOCTYPE html>
<html{% block html_attribs %}{% endblock html_attribs %}>
{%- block html %}
<head>
{%- block head %}
<title>{% block title %}{{title|default}}{% endblock title %}</title>
{%- block metas %}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{%- endblock metas %}
{%- block styles %}
<!-- Bootstrap -->
<link href="{{bootstrap_find_resource('css/bootstrap.css', cdn='bootstrap')}}" rel="stylesheet">
{%- endblock styles %}
{%- endblock head %}
</head>

一般来说是没办法在head中加入新的元素,这时候需要重写,如果想继续加载bootstrap的内容,就要用到super()。

{% block head %}
{{ super() }}
<link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.ico') }}" type="image/x-icon">
{% endblock %}

通过route设置

打开网站后,服务器会自动搜索host/favicon.ico,如果找到则使用作为icon。

@bp.route('/favicon.ico')
def get_fav():
    return current_app.send_static_file('img/favicon.ico')

以上两种操作的ico都在static/img下面

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值