Skip to content
Snippets Groups Projects
home.py 983 B
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
    
    from models import Person
    
    
    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 Person.get_all():
    
                    rows += str(e) + "<br>"
    
                return rows
    
            except Exception as e:
                return f"App database error: {e}"
    
        else:
    
            firstname = request.form.get('firstname')
    
    Alex's avatar
    Alex committed
            age = request.form.get('age')
    
    Alex's avatar
    Alex committed
            try:
    
                Person.add(firstname, int(age))
    
                return redirect(url_for2('.persons'))
            except Exception as e:
                return f"Could not add entity: {e}"