I am trying to pass a filename of an image and render it on a template,
Although I am passing the actual name through it does not display on the page
@app.route('/', methods=['GET','POST'])
@app.route('/start', methods=['GET','POST'])
def start():
person_to_show = 'tim'
profilepic_filename = os.path.join(people_dir, person_to_show, "img.jpg")
return render_template('start.html',profilepic_filename =profilepic_filename )
For example: profilepic_filename = /data/tim/img.jpg
I have tried
{{profilepic_filename}}
And I have also tried
Neither of which worked
解决方案
I have created people_photo in static folder and placed an image named shovon.jpg. From the application.py I passed the image as variable to template and showed it using img tag in the template.
In application.py:
from flask import Flask, render_template, request, url_for
import os
PEOPLE_FOLDER = os.path.join('static', 'people_photo')
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = PEOPLE_FOLDER
@app.route('/')
@app.route('/index')
def show_index():
full_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'shovon.jpg')
return render_template("index.html", user_image = full_filename)
In index.html:
Index
Output: