I have a "templates" folder and a "images" folder located in my project directory. Inside the templates folder I have the base.html file that is supposed to display the image located in the images folder. However, when i go on the website and open the console, it says: GET http://127.0.0.1:5000/images/address.png 404 (NOT FOUND). The image is not displayed.
main.py
from flask import Flask, render_template, request, redirect, session, url_for
from werkzeug.security import generate_password_hash, check_password_hash
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
# Routes
@app.route('/')
def index():
return redirect(url_for('home'))
@app.route('/home')
def home():
return render_template('base.html')
@app.route('/rd')
def rd():
return
@app.route('/about', methods=['GET'])
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(debug=True)
I have tried changing the directory to /images/address.png, ./images/address.png and etc., non worked
I expected the image to show up
You need to store your images in the /static
folder, and use the url_for
function to retrieve them in the html code.
You can also use app.static_folder=/path
to change the static folder path.
File structure
static
|- images
|- daniel-shapiro--QtglPvx3I0-unsplash.jpg
templates
|- index.html
main.py
main.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
if __name__ == '__main__':
app.run()
index.html
<h3>This is an apple</h3>
<img
src="{{ url_for('static', filename='images/daniel-shapiro--QtglPvx3I0-unsplash.jpg') }}"
class="image"
/>
<style>
.image {
width: 200px;
}
</style>