Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja2 Using images in HTML

I wasted a lot of time to find out what is wrong. I need your help now. I want to render template with image from my filesystem. But it is not working. Path - string that contains file name

@app.route('/', methods=['GET'])
def main():

    return render_template('main.html',image = path)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
     <title>test</title>
</head>
<body>
<h2></h2>
<form method="post" enctype="multipart/form-data" action="/uploader">
    <input type="file" name="file">
    <button type="submit">Upload</button>
</form>
<img src={{ url_for('static', filename = image) }} >//i can't figure how to change this to use {{image}}
</body>
</html>
like image 952
Jan Koval Avatar asked Sep 06 '25 02:09

Jan Koval


1 Answers

Just generate full image path in your view and pass it to your template

@app.route('/', methods=['GET'])
def main():
    #path is filename string
    image_file = url_for('static', filename=path)

    return render_template('main.html', image_file=image_file)

and then just use it as full link

<img src={{ image_file}} >

if you have image file (with filename which stored in path) in your static folder this should work

like image 163
marzique Avatar answered Sep 07 '25 15:09

marzique