【问题标题】:Images appearing in all but 1 flask page图像出现在除 1 个烧瓶页面之外的所有页面中
【发布时间】: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 表行的例子或print data 有什么吗?
  • 在这个表达式中:cursor.execute("SELECT * FROM shoes WHERE id=id") 是你的item 视图函数的id 真的被传递给你的sql 表达式了吗?
  • 我认为你的sql 表达式应该是:cursor.execute("SELECT * FROM shoes WHERE id=%d" % id)

标签: python mysql flask


【解决方案1】:

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')

【讨论】:

    【解决方案2】:

    在您的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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多