Skip to content Skip to sidebar Skip to footer

Images Appearing In All But 1 Flask Page

I am creating a web app in flask, python, mysql. When viewing every other page on my website my images load, but when viewing one specific page, I can't get any images to load usin

Solution 1:

URLs are made up of directories and filenames. Anything that precedes a / is considered a directory. Anything after the final / is the filename. Your problem is that you're using relative URLs. When you say

static/pics/gamma.png

your browser makes a request for that file relative to the current page's directory. In the case of URLs like / and /shop, the directory is /. The browser will request /static/pics/gamma.png.

In the case of URLs like /shop/item/1, the directory is /shop/item/. Your browser will then request /shop/item/static/pics/gamma.png.

Since your URLs match the former, you should store them as absolute URLs (with the leading /) so that the browser will make the correct request.

On a semi-related note, you should be using url_for whenever possible.

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

Solution 2:

In your item view function, you are not passing the value of id into your sql expression, here is the safe way to do it:

@app.route('/shop/item/<id>')defitem(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)

Post a Comment for "Images Appearing In All But 1 Flask Page"