Skip to content
Snippets Groups Projects
home.py 1.03 KiB
Newer Older
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:
        req_form = request.form
        firstname = req_form.get('firstname')
        age = req_form.get('age')
        surname = req_form.get("surname")
Alex's avatar
Alex committed
        try:
            Person.add(firstname, surname, int(age))
            return redirect(url_for2('.persons'))
        except Exception as e:
            return f"Could not add entity: {e}"