Python Flask 图标未显示
Python Flask favicon not showing up
在过去的 45 分钟里,我一直在尝试弄清楚如何让我的网站图标出现在我的 Flask 页面上。
这是我当前的代码:
from flask import Flask, render_template, send_from_directory, url_for
import os
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html")
@app.route("/store")
def store():
return render_template("store.html")
@app.route("/games")
def games():
return render_template("games.html")
@app.route("/americanstudios")
def americanstudios():
return render_template("as.html")
@app.route("/pear")
def pear():
return render_template("pear.html")
@app.route("/favicon.ico")
def fav():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', minetype='image/vnd.microsof.icon')
if __name__ == "__main__":
app.run(debug=True)
基础 html 是这样的:
<!DOCTYPE html>
<html>
<title>{% block title %}{% endblock %}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font- awesome.min.css">
<head>
<link rel="shortcut icon" href="{{ url_for('static',filename='favicon.ico') }}">
</head>
<style>
body,h1,h2,h3,h4,h5,h6 {font-family: "Raleway", Arial, Helvetica, sans-serif}
</style>
<body class="w3-light-grey">
<!-- Navigation Bar -->
<div class="w3-bar w3-white w3-large">
<a href="/" class="w3-bar-item w3-button w3-red w3-mobile"></i>newtech</a>
<a href="store" class="w3-bar-item w3-button w3-mobile">Store</a>
<a href="games" class="w3-bar-item w3-button w3-mobile">Games</a>
<a href="americanstudios" class="w3-bar-item w3-button w3-mobile">American Studios</a>
<a href="pear" class="w3-bar-item w3-button w3-mobile">PEAR Electronics</a>
<a href="login" class="w3-bar-item w3-button w3-right w3-light-grey w3-mobile">Log In</a>
</body>
<div class="w3-main" style="margin-left:250px">
<div class="w3-row w3-padding-64">
<div class="w3-twothird w3-container">
{% block content %}
{% endblock %}
</div>
</div>
</div>
</html>
这是index.html
{% extends "base.html" %}
{% block title %}newtech inc.{% endblock %}
{% block content %}
<h1 class="w3-text-teal">Home</h1>
<p>Welcome to newtech inc, where we have the coolest new items for all purposes. Whether it's a new movie to watch, or a fun game, or maybe something else, we're here for you.</p>
{% endblock %}
base.html 中 head 中唯一的一行应该从静态文件夹加载我的网站图标作为页面的网站图标。但出于某种原因,该网站图标仍然是索引页面和所有其他页面的默认网站图标。
我的图标是一个 64 X 64 .ico 文件
它位于我的静态文件夹中
是的,静态文件夹与 main.py
位于同一目录中
但即便如此,网站图标仍未显示。
是的,我是 运行 正确的 main.py
也试过这个
但运气不好。谁能帮帮我?
您是否已经查看了 Flask 文档?他们推荐的参考资料与您那里的参考资料略有不同:
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
https://flask.palletsprojects.com/en/1.1.x/patterns/favicon/?highlight=favicon
要查找网络错误,您需要查看开发工具的网络选项卡。它表明您对 favicon.ico 的获取失败并显示 404。修复了吗?将静态文件夹添加到您的应用程序(请参阅代码中的 1),并添加获取图标和 return 文件 - 请参阅代码中的 2。
from flask import Flask,render_template,send_from_directory
app = Flask(__name__, static_folder='static') # 1 ( either add here or add through some os join)
import os
@app.route("/")
def home():
return render_template("index.html")
@app.route("/static/favicon.ico") # 2 add get for favicon
def fav():
print(os.path.join(app.root_path, 'static'))
return send_from_directory(app.static_folder, 'favicon.ico') # for sure return the file
if __name__=="__main__":
app.run(debug=True)
文件夹结构如下:
在过去的 45 分钟里,我一直在尝试弄清楚如何让我的网站图标出现在我的 Flask 页面上。
这是我当前的代码:
from flask import Flask, render_template, send_from_directory, url_for
import os
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html")
@app.route("/store")
def store():
return render_template("store.html")
@app.route("/games")
def games():
return render_template("games.html")
@app.route("/americanstudios")
def americanstudios():
return render_template("as.html")
@app.route("/pear")
def pear():
return render_template("pear.html")
@app.route("/favicon.ico")
def fav():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', minetype='image/vnd.microsof.icon')
if __name__ == "__main__":
app.run(debug=True)
基础 html 是这样的:
<!DOCTYPE html>
<html>
<title>{% block title %}{% endblock %}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font- awesome.min.css">
<head>
<link rel="shortcut icon" href="{{ url_for('static',filename='favicon.ico') }}">
</head>
<style>
body,h1,h2,h3,h4,h5,h6 {font-family: "Raleway", Arial, Helvetica, sans-serif}
</style>
<body class="w3-light-grey">
<!-- Navigation Bar -->
<div class="w3-bar w3-white w3-large">
<a href="/" class="w3-bar-item w3-button w3-red w3-mobile"></i>newtech</a>
<a href="store" class="w3-bar-item w3-button w3-mobile">Store</a>
<a href="games" class="w3-bar-item w3-button w3-mobile">Games</a>
<a href="americanstudios" class="w3-bar-item w3-button w3-mobile">American Studios</a>
<a href="pear" class="w3-bar-item w3-button w3-mobile">PEAR Electronics</a>
<a href="login" class="w3-bar-item w3-button w3-right w3-light-grey w3-mobile">Log In</a>
</body>
<div class="w3-main" style="margin-left:250px">
<div class="w3-row w3-padding-64">
<div class="w3-twothird w3-container">
{% block content %}
{% endblock %}
</div>
</div>
</div>
</html>
这是index.html
{% extends "base.html" %}
{% block title %}newtech inc.{% endblock %}
{% block content %}
<h1 class="w3-text-teal">Home</h1>
<p>Welcome to newtech inc, where we have the coolest new items for all purposes. Whether it's a new movie to watch, or a fun game, or maybe something else, we're here for you.</p>
{% endblock %}
base.html 中 head 中唯一的一行应该从静态文件夹加载我的网站图标作为页面的网站图标。但出于某种原因,该网站图标仍然是索引页面和所有其他页面的默认网站图标。
我的图标是一个 64 X 64 .ico 文件
它位于我的静态文件夹中
是的,静态文件夹与 main.py
位于同一目录中但即便如此,网站图标仍未显示。
是的,我是 运行 正确的 main.py
也试过这个
但运气不好。谁能帮帮我?
您是否已经查看了 Flask 文档?他们推荐的参考资料与您那里的参考资料略有不同:
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
https://flask.palletsprojects.com/en/1.1.x/patterns/favicon/?highlight=favicon
要查找网络错误,您需要查看开发工具的网络选项卡。它表明您对 favicon.ico 的获取失败并显示 404。修复了吗?将静态文件夹添加到您的应用程序(请参阅代码中的 1),并添加获取图标和 return 文件 - 请参阅代码中的 2。
from flask import Flask,render_template,send_from_directory
app = Flask(__name__, static_folder='static') # 1 ( either add here or add through some os join)
import os
@app.route("/")
def home():
return render_template("index.html")
@app.route("/static/favicon.ico") # 2 add get for favicon
def fav():
print(os.path.join(app.root_path, 'static'))
return send_from_directory(app.static_folder, 'favicon.ico') # for sure return the file
if __name__=="__main__":
app.run(debug=True)
文件夹结构如下: