Sep-21-2020, 01:02 AM
Hello to All,
Would you help resolve the following problem. The Flask web app I'm working on takes a screenshot (say of my computer - and it does work) and then insert/save that screenshot to Postgres db, which then the web app retrieves for displaying on the Jinja2 template. The seems to run with no errors but no saving to the db takes place. Thank for the help. This is my code:
Would you help resolve the following problem. The Flask web app I'm working on takes a screenshot (say of my computer - and it does work) and then insert/save that screenshot to Postgres db, which then the web app retrieves for displaying on the Jinja2 template. The seems to run with no errors but no saving to the db takes place. Thank for the help. This is my code:
from application import app from flask import Flask, render_template, url_for, redirect from flask_sqlalchemy import SQLAlchemy import pyscreenshot import random import string app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password_here@localhost:5432/database' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy() db.init_app(app) class Image(db.Model): __tablename__ = "images" id = db.Column(db.Integer, autoincrement=True, primary_key=True) image = db.Column(db.String, nullable=True) def __repr__(self): return '<id={},image={}>'.format(self.id, self.image) #====================================================================== def get_images(params=None): if not params: return Image.query.all() #====================================================================== db.create_all() @app.route('/') @app.route('/homepage') def homepage(): return render_template('homepage.html') @app.route('/get_screenshot', methods=['POST']) def get_screenshot(): im = pyscreenshot.grab() im.save('Screenshot3'): photo = Image(im) db.session.add(photo) db.session.commit() return render_template('homepage.html') @app.route('/images/db/', methods=['GET']) def get_images_from_db(): images = get_images()#function to help retrieve images from db return render_template('show_images.html', images=images, target='db')