簡體   English   中英

使用 Flask Python 將圖像從目錄顯示到 html 頁面

[英]Displaying image from directory to html page using Flask Python

在這里,我想將目錄中的圖像插入 html 頁面。 我知道如何使用確切的文件名來執行此操作,但現在我正在嘗試使用引用圖像名稱的變量來執行此操作。

這是我的 python 文件,我將圖像名稱保存到數據庫中。 圖片已經保存在之前的目錄中了。

//code.py
@app.route('/add_picture', methods=['POST'])
def add_picture():
    if 'user_id' in session:
        session['logged_in'] = True
        photo = request.form.get('photo')

        with sqlite3.connect('memory.db') as conn:
            cursor = conn.cursor()
            name = session['name']
            cursor.execute('''UPDATE users SET photo=? WHERE name=?''', (photo, name))
            conn.commit()

        return redirect("/settings")
    else:
        return redirect('/')
//code.html
<img src="{{ url_for('.static', filename='images/IMG_2165.JPG') }}" width="200" height="200"> 
//we inster the picture by using the image name and it works

<img src="{{ url_for('.static', filename='images/{{ photo }}') }}" width="200" height="200"> 
//displaying the picture using the variable which refers to the image name but it returns nothing...

1- 圖像之前已經保存在目錄中。

2-我使用變量“photo”將圖像名稱保存到數據庫中。

3- 變量“photo”正在從另一個函數返回。

4- 在 html 中,我想使用變量而不是圖像名稱“IMG_2165.JPG”來顯示圖像。

您的函數不應該將圖像的路徑作為變量return , photo, redirect('/') (如return , photo, redirect('/') )?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM