Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjun cheng wong
17,021 PointsUploaded image not shown on html
I want to create a simple gallery for personal use and I use flask-sqlalchemy to build this application. The application will allow me to upload images and show them on the homepage(index.html) The image is not shown on the HTML page, I think something is wrong with the database when I upload the image
Below is my code:
from flask import render_template, url_for, request, redirect
from models import db, Image, app
@app.route('/')
def index():
images = Image.query.all()
return render_template('index.html', images=images)
@app.route('/add_image/', methods=['GET', 'POST'])
def add_image():
if request.form:
new_image = Image(url=request.form['image'])
db.session.add(new_image)
db.session.commit()
return redirect(url_for('index'))
return render_template('add_image.html')
if __name__ == '__main__':
db.create_all()
app.run(debug=True, port=8000, host='0.0.0.0')
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///image.db'
db = SQLAlchemy(app)
class Image(db.Model):
id = db.Column(db.Integer, primary_key=True)
url = db.Column('URL', db.String())
def __repr__(self):
return f'''<Image(URL: {self.url})'''