-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
47 lines (32 loc) · 1.15 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Demo app using SQLAlchemy."""
from flask import Flask, request, redirect, render_template
from models import db, connect_db, Pet
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///sqla_intro'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
connect_db(app)
from flask_debugtoolbar import DebugToolbarExtension
app.config['SECRET_KEY'] = "SECRET!"
debug = DebugToolbarExtension(app)
@app.route("/")
def list_pets():
"""List pets and show add form."""
pets = Pet.query.all()
return render_template("list.html", pets=pets)
@app.route("/", methods=["POST"])
def add_pet():
"""Add pet and redirect to list."""
name = request.form['name']
species = request.form['species']
hunger = request.form['hunger']
hunger = int(hunger) if hunger else None
pet = Pet(name=name, species=species, hunger=hunger)
db.session.add(pet)
db.session.commit()
return redirect(f"/{pet.id}")
@app.route("/<int:pet_id>")
def show_pet(pet_id):
"""Show info on a single pet."""
pet = Pet.query.get_or_404(pet_id)
return render_template("detail.html", pet=pet)