url_for() 不呈现子目录中的文件
url_for() does not render files in sub directories
在我的 .html 文件中,我使用了:
<img href="{{ url_for('static', filename='/images/logo.png') }}" alt="highbrow">
这不是渲染图像。
我的文件结构如下所示:
/projects
/highbrow
app.py
/highbrow
__init__.py
main.py
/templates
/static
(bunch of .css files)
/js
(bunch of .js files)
/images
(bunch of images)
init.py 就像:
create_app():
app = Flask(__name__)
bcrypt.init_app(app)
login_manager.init_app(app)
return app
有问题的路由函数如下:
@route("/post/<string:post_id>", methods=["GET", "POST"])
def post(post_id):
post = fetch_post(post_id) # fetches the post from database
comment_form = PostForm()
if comment_form.validate_on_submit() and request.method == "POST":
comment_entry = {
"username": "foobar",
"user_profile_link": "/foobar",
"time": 0,
"comment": comment_form.comment.data
}
comments.append(comment_entry)
return render_template("post.html", comment_form=comment_form, comments=comments,
number_of_comments=post["comments"], post_details=post, notifications=notifications)
在我的 html 文件中,它是这样的:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Highbrow</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="" />
<meta name="keywords" content="" />
<link rel=icon type="image/x-icon" href="{{ url_for('static', filename='images/logo.png') }}">
<link rel=stylesheet type="text/css" href="{{ url_for('static', filename='bootstrap.min.css') }}">
<link rel=stylesheet type="text/css" href="{{ url_for('static', filename='line-awesome.css') }}">
<link rel=stylesheet type="text/css" href="{{ url_for('static', filename='line-awesome-font-awesome.min.css') }}">
<link rel=stylesheet type="text/css" href="{{ url_for('static', filename='font-awesome.min.css') }}">
<link rel=stylesheet type="text/css" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="wrapper">
<header>
<div class="container">
<div class="header-data">
<div class="logo">
<a href="/" title=""><img href="{{ url_for('static', filename='/images/logo.png') }}" alt="highbrow"></a>
</div><!--logo end-->
...
...
.css 文件渲染得很好,但 img 不渲染。它只显示替代文字。就像这样:
这里应该有logo.png。
控制台显示:
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /post/0562262d-85f3-45d1-bf64-f2f539653468 HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/bootstrap.min.css HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/line-awesome.css HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/line-awesome-font-awesome.min.css HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/font-awesome.min.css HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/style.css HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/fonts/line-awesome.woff2?v=1.1. HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:36] "GET /static/images/logo.png HTTP/1.1" 200 -
即使有更多图像和 .js 文件要渲染,它也只是停在这里。请注意最后的 GET 它显示状态 200 但仍未加载图像。
您不需要添加额外的前导斜线。您必须使用 src
属性代替 href
代码应该是:
<img src="{{ url_for('static', filename='images/logo.png') }}" alt="highbrow">
在我的 .html 文件中,我使用了:
<img href="{{ url_for('static', filename='/images/logo.png') }}" alt="highbrow">
这不是渲染图像。 我的文件结构如下所示:
/projects
/highbrow
app.py
/highbrow
__init__.py
main.py
/templates
/static
(bunch of .css files)
/js
(bunch of .js files)
/images
(bunch of images)
init.py 就像:
create_app():
app = Flask(__name__)
bcrypt.init_app(app)
login_manager.init_app(app)
return app
有问题的路由函数如下:
@route("/post/<string:post_id>", methods=["GET", "POST"])
def post(post_id):
post = fetch_post(post_id) # fetches the post from database
comment_form = PostForm()
if comment_form.validate_on_submit() and request.method == "POST":
comment_entry = {
"username": "foobar",
"user_profile_link": "/foobar",
"time": 0,
"comment": comment_form.comment.data
}
comments.append(comment_entry)
return render_template("post.html", comment_form=comment_form, comments=comments,
number_of_comments=post["comments"], post_details=post, notifications=notifications)
在我的 html 文件中,它是这样的:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Highbrow</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="" />
<meta name="keywords" content="" />
<link rel=icon type="image/x-icon" href="{{ url_for('static', filename='images/logo.png') }}">
<link rel=stylesheet type="text/css" href="{{ url_for('static', filename='bootstrap.min.css') }}">
<link rel=stylesheet type="text/css" href="{{ url_for('static', filename='line-awesome.css') }}">
<link rel=stylesheet type="text/css" href="{{ url_for('static', filename='line-awesome-font-awesome.min.css') }}">
<link rel=stylesheet type="text/css" href="{{ url_for('static', filename='font-awesome.min.css') }}">
<link rel=stylesheet type="text/css" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="wrapper">
<header>
<div class="container">
<div class="header-data">
<div class="logo">
<a href="/" title=""><img href="{{ url_for('static', filename='/images/logo.png') }}" alt="highbrow"></a>
</div><!--logo end-->
...
...
.css 文件渲染得很好,但 img 不渲染。它只显示替代文字。就像这样:
这里应该有logo.png。
控制台显示:
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /post/0562262d-85f3-45d1-bf64-f2f539653468 HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/bootstrap.min.css HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/line-awesome.css HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/line-awesome-font-awesome.min.css HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/font-awesome.min.css HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/style.css HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:35] "GET /static/fonts/line-awesome.woff2?v=1.1. HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2021 00:16:36] "GET /static/images/logo.png HTTP/1.1" 200 -
即使有更多图像和 .js 文件要渲染,它也只是停在这里。请注意最后的 GET 它显示状态 200 但仍未加载图像。
您不需要添加额外的前导斜线。您必须使用 src
属性代替 href
代码应该是:
<img src="{{ url_for('static', filename='images/logo.png') }}" alt="highbrow">