2016-07-09 1 views
0

플라스크, 파이썬, mysql에서 웹 앱을 만들고 있습니다. 내 웹 사이트에서 다른 모든 페이지를 볼 때 내 이미지가로드되지만 특정 페이지를 볼 때 이미 작동중인 코드를 사용하여 이미지를로드 할 수 없습니다. 어떤 이유가있을 수 있습니까?플라스크 페이지 1 개를 제외한 모든 페이지에 나타나는 이미지

내 작업 페이지 중 하나에 대한 코드 :

{% 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 %} 

과 파이썬 파일 :

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 %} 

과 파이썬 파일 :

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;

빈 페이지에서 요소를 검사 할 때 작업 페이지와 동일한 src URL을 가지고 있기 때문에 SQL 데이터가이 페이지로 올바르게 전송되고 있음을 알고 있습니다. 또한, 내가 가져온 행의 다른 모든 속성은 정확합니다. 내 SQL 테이블은 다음과 같습니다

당신의 item보기 기능에서
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 
+0

SQL 테이블 행의 예나'print'에'data'가 포함될 수 있습니까? –

+0

그리고이 표현식에서'cursor.execute ("SELECT * FROM shoes WHERE id = id")'는 실제로 당신의 SQL 표현식으로 전달되는'item' 뷰 함수의'id'입니까? –

+0

당신의'sql' 표현식은 다음과 같아야한다고 생각합니다 :'cursor.execute ("SELECT * FROM shoes where id = % d"% id)' –

답변

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 (선두가 /)로 저장해야합니다.

준 관련 메모에서 가능한 경우 url_for을 사용해야합니다.

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

, 당신은 당신의 sql 표현에 id의 값을 전달하지 않는, 여기 할 수있는 안전한 방법입니다 :

@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) 
관련 문제