gpt4 book ai didi

python - 图像出现在除 1 个 flask 页面之外的所有页面中

转载 作者:行者123 更新时间:2023-11-29 07:14:57 24 4
gpt4 key购买 nike

我正在使用 Flask、Python、MySQL 创建一个 Web 应用程序。当查看网站上的所有其他页面时,我的图像会加载,但是当查看一个特定页面时,我无法使用已经工作的代码加载任何图像。这可能有什么原因吗?

我的工作页面之一的代码:

{% extends "base.html" %}

{% block content %}

<!-- home style sheet -->
<link rel="stylesheet" type="text/css" href="/static/css/home.css">

{% for index in range(0, shoes|count, 3) %}

<div class="row">
<div class="col-md-4">
<a href="shop/item/{{ shoes[index][0] }}">
<h4>{{ shoes[index][1] }}</h4>
<img src="{{ shoes[index][7] }}" alt="" width="250px" height="150px">
</a>
</div>
{% if shoes|count - index >= 2 %}
<div class="col-md-4">
<a href="shop/item/{{ shoes[index][0] + 1 }}">
<h4>{{ shoes[index + 1][1] }}</h4>
<img src="{{ shoes[index + 1][7] }}" alt="" width="250px" height="150px">
</a>
</div>
{% endif%}
{% if shoes|count - index >= 3 %}
<div class="col-md-4">
<a href="shop/item/{{ shoes[index][0] + 2 }}">
<h4>{{ shoes[index + 2][1] }}</h4>
<img src="{{ shoes[index + 2][7] }}" alt="" width="250px" height="150px">
</a>
</div>
{% endif%}
</div>

{% endfor %}

{% endblock %}

及其Python文件:

from app import app, mysql
from flask import render_template

@app.route('/shop')
def shop():
cursor = mysql.connect().cursor()
cursor.execute("SELECT * FROM shoes")

data = cursor.fetchall()
return render_template('shop.html', shoes = data)

现在失败的页面:

{% extends "base.html" %}

{% block content %}

<!-- home style sheet -->
<link rel="stylesheet" type="text/css" href="/static/css/home.css">

<div class="row">
<div class="col-md-5">
<img src="{{ item[0][7] }}" alt="" width="250px" height="150px">
</div>
</div>

{% endblock %}

及其Python文件:

from app import app, mysql
from flask import render_template

@app.route('/shop/item/<id>')
def item(id):

cursor = mysql.connect().cursor()
cursor.execute("SELECT * FROM shoes WHERE id=id")

data = cursor.fetchall()
return render_template('item.html', item = data)

感谢任何帮助,谢谢。

编辑;

我知道 sql 数据已正确发送到此页面,因为当我检查空图像上的元素时,我具有与工作页面上完全相同的 src url。此外,我获取的行的所有其他属性都是正确的。我的 sql 表如下所示:

id | name                             | size | price  | primarycolor | secondarycolor | othercolor | img                     |
+----+----------------------------------+------+--------+--------------+----------------+------------+-------------------------+
| 1 | 2013 Air Jordan 11 Retro | 10.0 | 215.00 | black | gamma blue | NULL | static/pics/gamma.png

最佳答案

URL 由目录和文件名组成。 / 之前的任何内容都被视为目录。最后的 / 之后的任何内容都是文件名。您的问题是您使用的是相对 URL。当你说

static/pics/gamma.png

您的浏览器会请求相对于当前页面目录的该文件。对于 //shop 之类的 URL,目录为 /。浏览器将请求 /static/pics/gamma.png

对于像 /shop/item/1 这样的 URL,目录是 /shop/item/。然后,您的浏览器将请求 /shop/item/static/pics/gamma.png

由于您的 URL 与前者匹配,因此您应该将它们存储为绝对 URL(以 / 开头),以便浏览器发出正确的请求。

在半相关的说明中,您应该尽可能使用 url_for

url_for('static', filename='css/home.css')

关于python - 图像出现在除 1 个 flask 页面之外的所有页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38279228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com