Answer the question
In order to leave comments, you need to log in
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>
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question