查询 SQLite 时,我是否需要一个生成器,也许是用于 flask 循环的 SQLAlchemy?
Do I need a generator, maybe SQLAlchemy for flask loop when querying SQLite?
我试图在 flask for 循环中查询满足特定条件的 SQLite 和 return 图像路径,但我得到一个损坏的图像图标。 image_path 被保存到 SQLite table 中,我认为这是一个很好的解决方案。我正在使用的代码如下。第一组是我遇到问题的地方,第二组在将图像保存到 static
目录时起作用。我是否需要为此创建一个生成器,或者我只是在其他地方犯了错误?这就是 SQLite table 的样子,
编辑 1
我认为这可能有助于解释我在这里想要的东西。我希望 index.html
显示一个 Recent Ad 部分,该部分将显示最近发布的广告,例如 Adverts
app.py*
@app.route("/")
def index():
sqliteConnection = sqlite3.connect('finance.db')
cursor = sqliteConnection.cursor()
ads = cursor.execute("SELECT image_path FROM food WHERE active = 'Y'")
ads = cursor.fetchall()
print(ads)
return render_template("index.html", ads=ads)
这是 print(ads)
的输出
`[('/home/ubuntu/final/freeood/dan.jpg',), ('/home/ubuntu/final/freeood/dan3.jpg',)]'
接下来是代码
的结果
for row in ads:
print(*row, sep='\t')
/home/ubuntu/final/freeood/dan.jpg
/home/ubuntu/final/freeood/dan3.jpg
index.html
{% for ad in ads %}
<img src={{ad}}>
{% endfor %}
这会产生损坏的图标图像。我也试过添加`ads.
当使用以下代码将 jpeg 文件保存到 static
目录时,我可以显示图像,但是我遇到了问题 with saving to the static folder 所以我想找出解决方法以防万一
app.py
ads = os.listdir(os.path.join(app.static_folder, "images"))
return render_template("index.html", ads=ads)
index.html
{% for ad in ads %}
<img src='/static/images/{{ad}}'/>
{% endfor %}
图像文件应位于您网站的 static
文件夹中。这是因为所有文件路径都是相对于您网站的根目录。
但是,如果您确实需要使用 send_from_directory
访问网站外部的文件,则有一个解决方法。
将此函数添加到app.py:
from flask import send_from_directory
@app.route('/uploads/<path:img_path>')
def download_file(img_path):
directory, filename = img_path.rsplit('/', 1)
return send_from_directory(directory, filename, as_attachment=True)
然后你可以在你的模板中调用这个函数index.html:
{% for ad in ads %}
<img src="{{ url_for('download_file', img_path=ad) }}"/>
{% endfor %}
我试图在 flask for 循环中查询满足特定条件的 SQLite 和 return 图像路径,但我得到一个损坏的图像图标。 image_path 被保存到 SQLite table 中,我认为这是一个很好的解决方案。我正在使用的代码如下。第一组是我遇到问题的地方,第二组在将图像保存到 static
目录时起作用。我是否需要为此创建一个生成器,或者我只是在其他地方犯了错误?这就是 SQLite table 的样子,
编辑 1
我认为这可能有助于解释我在这里想要的东西。我希望 index.html
显示一个 Recent Ad 部分,该部分将显示最近发布的广告,例如 Adverts
app.py*
@app.route("/")
def index():
sqliteConnection = sqlite3.connect('finance.db')
cursor = sqliteConnection.cursor()
ads = cursor.execute("SELECT image_path FROM food WHERE active = 'Y'")
ads = cursor.fetchall()
print(ads)
return render_template("index.html", ads=ads)
这是 print(ads)
`[('/home/ubuntu/final/freeood/dan.jpg',), ('/home/ubuntu/final/freeood/dan3.jpg',)]'
接下来是代码
的结果for row in ads:
print(*row, sep='\t')
/home/ubuntu/final/freeood/dan.jpg
/home/ubuntu/final/freeood/dan3.jpg
index.html
{% for ad in ads %}
<img src={{ad}}>
{% endfor %}
这会产生损坏的图标图像。我也试过添加`ads.
当使用以下代码将 jpeg 文件保存到 static
目录时,我可以显示图像,但是我遇到了问题 with saving to the static folder 所以我想找出解决方法以防万一
app.py
ads = os.listdir(os.path.join(app.static_folder, "images"))
return render_template("index.html", ads=ads)
index.html
{% for ad in ads %}
<img src='/static/images/{{ad}}'/>
{% endfor %}
图像文件应位于您网站的 static
文件夹中。这是因为所有文件路径都是相对于您网站的根目录。
但是,如果您确实需要使用 send_from_directory
访问网站外部的文件,则有一个解决方法。
将此函数添加到app.py:
from flask import send_from_directory
@app.route('/uploads/<path:img_path>')
def download_file(img_path):
directory, filename = img_path.rsplit('/', 1)
return send_from_directory(directory, filename, as_attachment=True)
然后你可以在你的模板中调用这个函数index.html:
{% for ad in ads %}
<img src="{{ url_for('download_file', img_path=ad) }}"/>
{% endfor %}