Images appearing in all but 1 flask page

2024/10/14 0:29:39

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 using already working code. Is there any reason this may be?

Code for one of my working pages:

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

and its python file:

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)

Now the failing page:

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

and its python file:

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)

Any help is appreciated, thanks.

EDIT;

I know the sql data is being sent to this page correctly because when I inspect element on the empty image, I have the exact same src url as on the working page. Also, all other attributes of the row i fetched are correct. My sql table looks like this:

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 
Answer

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')
https://en.xdnf.cn/q/118020.html

Related Q&A

Python: Write nested list objects to csv file

Im trying to write data from a list of lists to a csv file. This is a simplified version of what I haveclass Point(object): def __init__(self, weight, height):self.weight = weightself.height = heightde…

Entire module is not detected by __init__.py

I have a relatively small python program which is setup like thisRoot--Network--DTOLots of py files which contain classes.Other py files in the projectBoth in the Network and the DTO folder there is an…

Python output to terminal during ssh login

I have been looking everywhere for this and have not found a solution. I am using python 2.5.1 on an Apple iPod and I am trying to connect to a host with SSH. First I start off with import os. Next I o…

Unable to find SIFT or xfeatures2d in OpenCV Python [duplicate]

This question already has answers here:PyCharm: Installation of non-free OpenCV modules for operations like SIFT, SURF(2 answers)Closed 6 years ago.I recently switch back to python for facial detection…

Django paginate for django 2

I need to use pagination to a Django list but I couldnt find any help online,, only old docs from Django version 1.3 here are my files : views.pydef home(request):all_dress = Item.objects.all().filter(…

AttributeError Button object has no attribute scrlFBtn

from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.core.window import Window from kivy.uix.scrollview import ScrollView from kivy.effects.scrol…

How to set interpreter of WinPython as the vim default python interpreter?

I use a Python distribution named WinPython. Now I want my vim to use the python interpreter in WinPython as its default interpreter. I tried add the F:\WinPython\python-2.7.3.amd64 into my windows env…

how to deal with Python BaseHTTPServer killed,but the port is still be occupied?

I use python BaseHTTPServer,it can handle do_GET,do_POST methods,in do_POST method,i execute linux shell using os.system,when i kill the python script,but the listening port still occupied,so i cant ru…

Circular imports and class fields in python3

Okay, I do understand that this topic is old as hell, but I couldnt find an answer to the particular question that I am asking.Lets say that we have a very simple structure: two files, a.py and b.py, t…

Bad HTTP response returned from the server. Code 500

I have a problem to use pywinrm on linux, to get a PowerShell Session. I read several posts and questions on sites about that. But any that can solve my question. The error is in the Kerberos autenti…