Skip to content
Snippets Groups Projects
home.py 1.12 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alex's avatar
    Alex committed
    from flask import Blueprint, render_template, request, redirect
    
    from config import url_for2, FIRST_DB_BIND_NAME
    from database.db import lookup_db_for_binding
    from models import Person
    
    db = lookup_db_for_binding(FIRST_DB_BIND_NAME)
    
    
    home_blueprint = Blueprint('home', __name__, url_prefix='/')
    
    
    @home_blueprint.route('')
    def home():
        return render_template('index.html')
    
    
    @home_blueprint.route('/hello/<name>')
    def hello(name: str):
        return "Hello, " + name
    
    
    # Example CRUD route
    
    
    @home_blueprint.route('/persons', methods=['GET', 'POST'])
    def persons():
    
        if request.method == "GET":
            try:
                rows = ""
    
                for e in db.session.query(Person).all():
    
                    rows += str(e) + "<br>"
    
                return rows
            except:
    
    Alex's avatar
    Alex committed
                return "App database error"
    
        else:
    
            firstname = request.form.get('firstname')
    
    Alex's avatar
    Alex committed
            age = request.form.get('age')
    
    Alex's avatar
    Alex committed
            try:
    
                db.session.add(Person(firstname=firstname, age=int(age)))
    
    Alex's avatar
    Alex committed
                db.session.commit()
    
                return redirect(url_for2('.persons'))
            except Exception as e:
                return f"Could not add entity: {e}"