图像出现在除1个烧瓶页面以外的所有页面

时间:2016-07-09 06:55:27

标签: python mysql flask

我正在使用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)

感谢任何帮助,谢谢。

EDIT;

我知道正确地将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 

2 个答案:

答案 0 :(得分:1)

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)

答案 1 :(得分:1)

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

static/pics/gamma.png

您的浏览器会针对当前页面的目录发出该文件的请求。对于//shop等网址,目录为/。浏览器将请求/static/pics/gamma.png

对于/shop/item/1等网址,目录为/shop/item/。然后,您的浏览器将请求/shop/item/static/pics/gamma.png

由于您的网址与前者匹配,因此您应将其存储为绝对网址(使用前导/),以便浏览器发出正确的请求。

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

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