Python 出现在除1页以外的所有页面中的图像
我正在用flask、python和mysql创建一个web应用程序。在我的网站上查看其他页面时,我会加载图像,但在查看一个特定页面时,我无法使用已经运行的代码加载任何图像。这有什么原因吗 我的一个工作页的代码:Python 出现在除1页以外的所有页面中的图像,python,mysql,flask,Python,Mysql,Flask,我正在用flask、python和mysql创建一个web应用程序。在我的网站上查看其他页面时,我会加载图像,但在查看一个特定页面时,我无法使用已经运行的代码加载任何图像。这有什么原因吗 我的一个工作页的代码: {% extends "base.html" %} {% block content %} <!-- home style sheet --> <link rel="stylesheet" type="text/css" href="/static/css/
{% 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 %}
现在是失败的一页:
{% 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 %}
在
item
查看函数中,您没有将id
的值传递到sql
表达式中,以下是安全的方法:
@app.route('/shop/item/<id>')
def item(id):
cursor = mysql.connect().cursor()
sql_exp = "SELECT * FROM shoes WHERE id=%d"
cursor.execute(sql_exp, id)
data = cursor.fetchall()
return render_template('item.html', item = data)
@app.route(“/shop/item/”)
def项目(id):
cursor=mysql.connect().cursor()
sql_exp=“从id=%d的鞋子中选择*”
cursor.execute(sql\u exp,id)
data=cursor.fetchall()
返回呈现模板('item.html',item=data)
在项
查看函数中,您没有将id
的值传递到sql
表达式中,以下是安全的方法:
@app.route('/shop/item/<id>')
def item(id):
cursor = mysql.connect().cursor()
sql_exp = "SELECT * FROM shoes WHERE id=%d"
cursor.execute(sql_exp, id)
data = cursor.fetchall()
return render_template('item.html', item = data)
@app.route(“/shop/item/”)
def项目(id):
cursor=mysql.connect().cursor()
sql_exp=“从id=%d的鞋子中选择*”
cursor.execute(sql\u exp,id)
data=cursor.fetchall()
返回呈现模板('item.html',item=data)
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\u
url_for('static', filename='css/home.css')
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\u
url_for('static', filename='css/home.css')
您是否可以包含sql表行的示例或print
whatdata
have?在这个表达式中:cursor.execute(“SELECT*FROM shoes WHERE id=id”)
查看函数的id
是否真的传递给了您的sql表达式?我认为您的sql
表达式应该是:cursor.execute(“从鞋中选择*,其中id=%d”%id)
您可以包含sql表行的示例或打印数据的示例吗?在这个表达式中:cursor.execute(“SELECT*FROM shoes WHERE id=id”)
查看函数的id
是否真的传递给了sql表达式?我认为sql
表达式应该是:cursor.execute(“SELECT*FROM shoes WHERE id=%d”%id)