如何解决url_for() 不渲染子目录中的文件
在我的 .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 但仍然没有加载图像。
解决方法
您不需要添加额外的前导斜杠。您必须使用 <img src="<?php echo $_SERVER['DOCUMENT_ROOT'] . 'Romeo/Yoomak/pix/Logo9.png';?>" width="200" height="200" alt="Logo"/>
属性代替 src
代码应该是:
href
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。