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)

初始化的.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">

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章