【发布时间】:2016-07-09 06:55:27
【问题描述】:
我正在烧瓶、python、mysql 中创建一个网络应用程序。在查看我网站上的所有其他页面时,我的图像会加载,但是在查看一个特定页面时,我无法使用已经工作的代码加载任何图像。这有什么原因吗?
我的一个工作页面的代码:
{% 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
【问题讨论】:
-
你能包括一个你的 sql 表行的例子或
printdata有什么吗? -
在这个表达式中:
cursor.execute("SELECT * FROM shoes WHERE id=id")是你的item视图函数的id真的被传递给你的sql 表达式了吗? -
我认为你的
sql表达式应该是:cursor.execute("SELECT * FROM shoes WHERE id=%d" % id)