Skip to content
Snippets Groups Projects
home.py 1.1 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
    
    from database.db import db
    from models.user import Entity
    
    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('/entities', methods=['GET', 'POST'])
    def entities():
        if request.method == "GET":
            try:
                rows = ""
                for e in db.session.query(Entity).all():
                    rows += str(e) + "<br>"
    
                return rows
            except:
                return "App database error (have you setup a database for this app?)"
        else:
            username = request.form.get('username')
            email = request.form.get('email')
    
    
    Alex's avatar
    Alex committed
            try:
                db.session.add(Entity(username=username, email=email))
                db.session.commit()
                return redirect(url_for2('.entities'))
            except:
                return "Could not add entity, username and/or email are already in use."