Skip to content
Snippets Groups Projects
Commit 234f7263 authored by Michael Kyriakou's avatar Michael Kyriakou
Browse files

Added "Persons" blueprint, separating out the "Person" specific routes

from the "Home" blueprint. Improved README.
parent ffd0c95a
No related branches found
No related tags found
No related merge requests found
......@@ -38,20 +38,9 @@ If you navigate to `http://localhost:5000`, you will see the response created by
You will also notice the lines `Environment: production` and `Debug mode: off` when the Flask application starts in the console. To enable debug mode, you must set the environment variable `ENV` to `dev`, ie: `export ENV=dev` (see `config/config.py` for more details on different environments).
# Flask Routes
A "route manager" in Flask is called a "Blueprint", all blueprints need to be "registered" in `app.py`, otherwise they will not be accessible. A "router manager" has the role of handling requests from all routes that start with the provided "url_prefix".
## Tutorial: Adding a new blueprint
TODO
## Tutorial: Adding a new route
TODO
Example: `items = Blueprint("items", __name__, url_prefix="/inventory/items")`
## Tutorial: Adding database interaction
TODO
A "route manager" ("router") in Flask is called a "Blueprint", it has the role of handling requests from all routes that start with the provided "url_prefix". It can be thought of as a "subrouter", with the "superrouter" being the Flask app itself, checking for a match against all registered "subrouters".
All blueprints need to be "registered" in `app.py`, otherwise they will not be considered when path matching, and therefore will not be accessible.
# Flask Migrations and Databases
A table in flask is called a "Model", all models need to extend "db.Model". You have been provided with two models, "Person" and "PhoneNumber" which is a 1-many relationship; a "Person" can be many "PhoneNumber".
......
......@@ -37,9 +37,10 @@ init_all_dbs_with_app(app)
from models import *
# Register all blueprints/routes
from blueprints import home_blueprint
from blueprints import home_blueprint, persons_blueprint
# from blueprints.auth import auth_blueprint
app.register_blueprint(home_blueprint)
app.register_blueprint(persons_blueprint)
# app.register_blueprint(auth_blueprint)
# ===================== MIGRATION SETUP =====================
......
from .home import home_blueprint
\ No newline at end of file
from .home import home_blueprint
from .first_db_blueprints import persons_blueprint
from .persons import persons_blueprint
from flask import Blueprint, request, redirect
from config import url_for2
from models import Person
persons_blueprint = Blueprint('persons', __name__, url_prefix='/first_db/persons')
# Example CRUD route
@persons_blueprint.route('/create-person', methods=['POST'])
def create_person():
req_form = request.form
firstname = req_form.get('firstname')
age = req_form.get('age')
surname = req_form.get("surname")
try:
Person.add(firstname, surname, int(age))
return redirect(url_for2('.display_all_persons'))
except Exception as e:
return f"Could not add entity: {e}"
@persons_blueprint.route('/display-all-persons', methods=['GET'])
def display_all_persons():
try:
rows = ""
for e in Person.get_all():
rows += str(e) + "<br>"
return rows
except Exception as e:
return f"App database error: {e}"
from flask import Blueprint, render_template, request, redirect
from config import url_for2
from models import Person
from flask import Blueprint, render_template
home_blueprint = Blueprint('home', __name__, url_prefix='/')
@home_blueprint.route('')
def home():
return render_template('index.html')
......@@ -13,29 +10,3 @@ def home():
@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")
try:
Person.add(firstname, surname, int(age))
return redirect(url_for2('.persons'))
except Exception as e:
return f"Could not add entity: {e}"
......@@ -22,7 +22,7 @@
</p><br>
<h3>Example CRUD form - Create a record in the database:</h3>
<form method="post" action="{{ url('home.persons') }}">
<form method="post" action="{{ url('persons.create_person') }}">
Firstname:<br>
<input type="text" name="firstname">
<br>
......@@ -35,6 +35,6 @@
<button type="submit">Create Entry</button>
</form>
<br>
<a href="{{ url('home.persons') }}">View records</a>
<a href="{{ url('persons.display_all_persons') }}">View records</a>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment