This is CS50
Scan your ID for attendance
Think.
Pair.
Share.
View
Controller
View
Controller
View
Model
birthdays/
static/
styles.css
templates/
index.html
app.py
birthdays.db
Routes
https://birthdays.net/
https://birthdays.net/add
@app.route("/")
def index():
# Return index page ("homepage")
@app.route("/")
def index():
return render_template("index.html")
flask run
Forms
<form action="/" method="post">� ...�</form>
Route to request
Request method
Form element
<input name="friend" type="text">
<input name="month" type="number">
<button type="submit">Submit</button>
Elements of forms
<input name="friend" type="text">
<input name="month" type="number">
<button type="submit">Submit</button>
Elements of forms
Submits form when clicked!
Download birthdays…
Add a form
In index.html, add a form to submit new birthdays. The form should have the following attributes:
Updating a model
request.form.get("friend")
<input name="friend" type="text">
POST
request.args.get("friend")
<input name="friend" type="text">
GET
INSERT INTO table (column1, column2)
VALUES (value1, value2);
db.execute()
db.execute("SELECT * FROM birthdays
WHERE month = ?", month)
db.execute("SELECT * FROM birthdays
WHERE month = ?
AND day = ?", month, day)
Update a database
In app.py, insert a new entry to the birthdays table of birthdays.db when the user submits data via POST:
Template Rendering
render_template("index.html",
message="Hello")
<p>The message is: {{ message }}</p>
render_template("index.html",
bdays=["Feb 2", "Feb 28"])
{% for bday in bdays %}
<p>{{ bday }}</p>
{% endfor %}
<p>This birthday is in {{ bday.month }}</p>
render_template("index.html",
bday={"month": "Feb", "day": 24})
{% ... %}
{{ ... }}
Jinja expressions (e.g., for)
Jinja variables (e.g., bday, message)
{% extends “layout.html” %}
{% block title %} {% endblock %}
{% block main %} {% endblock %}
Render birthdays
In app.py and index.html, query for birthdays and display them in a table.
Problem Set 9
Problem Set 9 Tips
Tutorials
Office Hours
This was CS50