我正在尝试在页面中呈现图像。我将图像的本地url存储在数据库中,以便在html文件中获取该url,从而显示图像。但实际情况是图像没有显示出来。你知道吗
我单独打开了html文件,并且成功地呈现了图像,但是在python代码中执行此操作并运行服务器时,图像有问题。你知道吗
This is a one item example of database declaration (python code)
cable1 = SectionItem(
name='Remax',
price='$3.99',
description='AUX Metal Cable - 3.5mm - 100Cm Male To Male',
image_url='img/cables/cable1.jpg',
category=cables
)
这个很好用。你知道吗
This is the SQLAlechemy for rendering the image
from flask import Flask, render_template, request, redirect, url_for,
flash
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database import Base
from database import Accessory, AccessorySection, SectionItem
app = Flask(__name__)
engine = create_engine('sqlite:///mobilystore.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
@app.route('/')
@app.route('/mobily')
def mobilystore():
all_cables = session.query(SectionItem).filter(
SectionItem.store_id == 1).all()
return render_template(
'mainpage.html', all_cables=all_cables)
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)
服务器也运行在终端没有问题。你知道吗
This is the html document in my templates folder
<html>
<body>
<h1>Mobily Store</h1>
</br>
{% for i in all_cables %}
<div>
<img src="{{i.image_url}}" alt="cable img">
<p> {{i.description}} </p>
<p> {{i.price}} </p>
</br>
</div>
{%endfor%}
</body>
</html>
显示价格和说明,但不显示图像。你知道吗
请注意,我将图像放在模板文件夹中的img文件夹中(同一文件夹包含我的html文件)。
这是github上的my-project,可以让您全面了解
您必须将
templates/img
文件夹移动到static/img
,然后在模板中使用url_for
函数打印图像路径:相关问题 更多 >
编程相关推荐