Adding a favicon to a Flask server without HTML

☆樱花仙子☆ 提交于 2020-01-03 09:29:31

问题


My flask server constantly reports

xx.xxx.xxx.xxx - - [DD/MM/YYYY HH:MM:SS] "GET /favicon.ico HTTP/1.1" 404 -

In the code for my flask server I've added,

@app.route('/favicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'),'favicon.ico', mimetype='image/vnd.microsoft.icon')

and I've added a favicon titled favicon.ico to the same directory that my flask server is running from.

Favicon location

If I try to navigate to http://www.myurl.com/favicon.ico I get a 404. My flask server isn't serving an html landing page so I can't add <link rel='shortcut icon' href='favicon.ico' type='image/x-icon'/ > anywhere. I don't really care about actually having a favicon, I just want to stop the error from showing up. How can I serve a favicon/stop the error?


回答1:


Put the icon in your static directory as favicon.ico. and below code in python file

import os
from flask import send_from_directory

@app.route('/favicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'),
                          'favicon.ico',mimetype='image/vnd.microsoft.icon')

href - http://flask.pocoo.org/docs/0.12/patterns/favicon/



来源:https://stackoverflow.com/questions/40696745/adding-a-favicon-to-a-flask-server-without-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!