A
A
anya_hacker2021-04-12 00:38:11
Flask
anya_hacker, 2021-04-12 00:38:11

How to put a photo in a Flask template?

I'm trying to insert a photo into a Flask template.
The projects folder contains:
1. The static folder (it contains the img folder with the robot.jpg photo)
2. The templates folder (it contains the index.html template).
3. Application launch code
The template itself:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
        <title>{{ title }}</title>
    </head>
    <body>
  <h2>{{ name }}</h2>
  <img src="{{ photo }}" alt="User Image">
    </body>
</html>


But the photo is not displayed.
What could be wrong?
The code itself:
from flask import Flask, render_template
import os

IMG_FOLDER = os.path.join('static', 'img')

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = IMG_FOLDER


@app.route('/index/<name>')
def index(name):
    full_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'robot.jpg')
    return render_template('index.html', title="Тест", name=name, photo=full_filename)


if __name__ == '__main__':
    app.run(port=8080, host='127.0.0.1')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
low molecular macro, 2021-04-12
@anya_hacker

<img src="{{ url_for('static', filename='photo') }}">

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question