问题
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