我有一個我已經構建的燒瓶應用程式。用戶可以通過表單上傳圖片。當用戶上傳圖片時,它會將圖片添加到檔案夾中website/static/images/。檔案正確保存在檔案夾中,但由于其位置或檔案夾結構的某些問題,我無法顯示影像。
這是初始化.py:
from flask import Flask, session, redirect, url_for
from pymongo import MongoClient
from os import path, mkdir
client = MongoClient()
db = client.Contractor
app = Flask(__name__, static_url_path='/static')
app.config['SECRET_KEY'] = 'eigeinsonriuosvnirosjvsoe'
upload_folder = 'website/static/images/'
if not path.exists(upload_folder):
mkdir(upload_folder)
app.config['MAX_CONTENT_LENGTH'] = 25 * 1024 * 1024
app.config['UPLOAD_FOLDER'] = upload_folder
這是我嘗試檢索它們并將它們顯示給用戶的地方:
{% for photo in all_photos %}
<div>
<a>
<img style="height: 250px;" src={{ url_for('static', filename=photo.filename ) }}/>
</a>
<p>{{ photo.image_name }}</p>
<p>{{ photo.caption }}</p>
<p>{{ photo.location }}</p>
</div>
{% endfor %}
我嘗試了以下方法:
<img style="height: 250px;" src={{ url_for('static', filename=photo.filename ) }}/>
還有這個
<img style="height: 250px;" src={{ url_for('static/images', filename=photo.filename ) }}/>
還有這個
<img style="height: 250px;" src={{ url_for('static/images/', filename=photo.filename ) }}/>
這是我嘗試任何變體時不斷收到的錯誤:
werkzeug.routing.BuildError: Could not build url for endpoint 'static/images' with values ['filename']. Did you mean 'static' instead?
uj5u.com熱心網友回復:
假設“網站”是設定 Flask 應用程式的主目錄,并且您有這樣的結構:
/website
/static
/images
那么你應該這樣做:
<img style="height: 250px;" src={{ url_for('static', filename='images/' photo.filename ) }}/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/380539.html
上一篇:燒瓶應用程式帶來錯誤requests.exceptions.ConnectionErrorrequests.exceptions.ConnectionErrorMaxretriesexceededwi
下一篇:我應該使用什么型別的連接
