图像出现在除1个烧瓶页面之外的所有页面中 [英] Images appearing in all but 1 flask page
问题描述
我正在用flask,python,mysql创建一个Web应用程序.在查看网站上的所有其他页面时,我的图像会加载,但是在查看一个特定页面时,我无法使用已经正常工作的代码来加载任何图像.可能有什么原因吗?
我的一个工作页面的代码:
{%扩展了"base.html"%}{%封锁内容%}<!-家庭样式表->< link rel ="stylesheet" type ="text/css" href ="/static/css/home.css">{%表示范围(0,鞋子|数量,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>{如果鞋子|计数-索引> = 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>{% 万一%}{如果鞋子|计数-索引> = 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>{% 万一%}</div>{%endfor%}{%endblock%}
及其python文件:
从应用程序导入应用程序从烧瓶导入render_template@ app.route('/shop')def shop():游标= mysql.connect().cursor()cursor.execute("SELECT * FROM shoes")数据= cursor.fetchall()返回render_template('shop.html',shoes = data)
现在出现故障页面:
{%扩展了"base.html"%}{%封锁内容%}<!-家庭样式表->< 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文件:
从应用程序导入应用程序从烧瓶导入render_template@ app.route('/shop/item/< id>')def item(id):游标= mysql.connect().cursor()cursor.execute("SELECT * FROM Shoes WHERE id = id")数据= cursor.fetchall()返回render_template('item.html',item = data)
感谢您的帮助.
编辑;
我知道sql数据已正确发送到此页面,因为当我检查空白图像上的元素时,我拥有与工作页面上完全相同的src url.另外,我获取的行的所有其他属性都是正确的.我的sql表看起来像这样:
id |名称|尺寸价格|原色|secondarycolor |其他颜色|img |+ ---- + ---------------------------------- + ------ +------- + -------------- + ---------------- + ------------+ ------------------------- +|1 |2013 Air Jordan 11 Retro10.0 |215.00 |黑色|伽玛蓝|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')
I am creating a web app in flask, python, mysql. When viewing every other page on my website my images load, but when viewing one specific page, I can't get any images to load using already working code. Is there any reason this may be?
Code for one of my working pages:
{% 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 %}
and its python file:
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)
Now the failing page:
{% 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 %}
and its python file:
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)
Any help is appreciated, thanks.
EDIT;
I know the sql data is being sent to this page correctly because when I inspect element on the empty image, I have the exact same src url as on the working page. Also, all other attributes of the row i fetched are correct. My sql table looks like this:
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
URLs are made up of directories and filenames. Anything that precedes a /
is considered a directory. Anything after the final /
is the filename. Your problem is that you're using relative URLs. When you say
static/pics/gamma.png
your browser makes a request for that file relative to the current page's directory. In the case of URLs like /
and /shop
, the directory is /
. The browser will request /static/pics/gamma.png
.
In the case of URLs like /shop/item/1
, the directory is /shop/item/
. Your browser will then request /shop/item/static/pics/gamma.png
.
Since your URLs match the former, you should store them as absolute URLs (with the leading /
) so that the browser will make the correct request.
On a semi-related note, you should be using url_for
whenever possible.
url_for('static', filename='css/home.css')
这篇关于图像出现在除1个烧瓶页面之外的所有页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!