我有一個燒瓶應用程式,用戶必須在其中上傳檔案,然后對其進行一些操作并顯示結果。
檔案大小限制為 2 MB。為此,我使用了
app.config['MAX_CONTENT_LENGTH'] = 2 * 1000 * 1000
Flask 中有 errorhandler 函式來顯示自定義錯誤訊息...
@app.errorhandler(413)
def page_not_found(e):
return "File should be less than 2 MB"
但它顯示在新頁面上。
我希望錯誤顯示在同一頁面上,以便用戶可以嘗試使用新檔案。就像現在一樣,要上傳新檔案,用戶必須按后退按鈕切換回上一頁,然后上傳檔案。
有沒有辦法做到這一點?比如,使用“閃光燈”或其他什么?
uj5u.com熱心網友回復:
可以使用閃光燈。首先,閃爍您的訊息,然后呈現模板。
@app.errorhandler(413)
def page_not_found(e):
flash("File should be less than 2 MB")
return render_template('myPage.html')
在您的 html 中,使用 Jinja:
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="popup">
<div class="text-center">
<script>window.alert('{{ messages[0] }}')</script>
</div>
</div>
{% endif %}
{% endwith %}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/375545.html
