1 of 45

This is CS50 �Week 9

2 of 45

Kelly Ding (she/her)

Preceptor

kelly@cs50.harvard.edu

3 of 45

4 of 45

View

5 of 45

6 of 45

Controller

View

7 of 45

8 of 45

Controller

View

Model

9 of 45

10 of 45

birthdays/

static/

styles.css

templates/

index.html

app.py

birthdays.db

11 of 45

Routes

12 of 45

https://example.com/

13 of 45

https://example.com/add

14 of 45

@app.route("/")

def index():

# Return index page

15 of 45

@app.route("/")

def index():

return render_template("index.html")

16 of 45

flask run

17 of 45

Forms

18 of 45

POST

  • POST is used to send data to a server to create/update a resource.

GET

  • GET is used to request data from a specified resource.

POST vs GET Request Methods

19 of 45

A user goes to a website and lands on the /login page.

20 of 45

A user goes to a website and lands on the /login page.

GET

21 of 45

A user enters their username and password and clicks Log In.

22 of 45

A user enters their username and password and clicks Log In.

POST

23 of 45

A user submits a birthday.

24 of 45

A user submits a birthday.

POST

25 of 45

Form element

<form action="/" method="post">� ...�</form>

Route to request

Request method

26 of 45

Elements of forms

<input name="friend" type="text">

<input name="month" type="number">

<button type="submit">...</button>

27 of 45

Elements of forms

<input name="friend" type="text">

<input name="month" type="number">

<button type="submit">...</button>

Submits form when clicked!

28 of 45

Add a form

In index.html, add a form to submit new birthdays. The form should have the following traits:

  • The form should submit to the default "/" route, using the POST method.
  • The form should include an input for a name, as well as the month and day of a birthday.
  • The form should include a button to submit the form.

29 of 45

Updating with db.execute

30 of 45

request.form.get("friend")

<input name="friend" type="text">

app.py

index.html

31 of 45

INSERT INTO table (column1, column2)

VALUES (value1, value2);

32 of 45

db = SQL("sqlite:///birthdays.db")

db.execute(...)

33 of 45

db.execute("SELECT * FROM birthdays WHERE month = ?", month)

34 of 45

db.execute("SELECT * FROM birthdays WHERE month = ? AND day = ?", month, day)

35 of 45

Update a database

In app.py, insert a new entry to the birthdays table of birthdays.db when the user submits data via POST:

  • Use request.form.get to get the values from each named input element.
  • Use db.execute with ? placeholders to insert those values into the birthdays table.
  • Be sure to validate user input. For example, check if a value is None before inserting the value into the database.

36 of 45

Template Rendering

37 of 45

render_template("index.html",

message="Hello")

<p>The message is: {{ message }}</p>

app.py

index.html

38 of 45

The message is: Hello

39 of 45

render_template("index.html",

list=["Apple", "Banana"])

{% for item in list %}

<p>{{ item }}</p>

{% endfor %}

app.py

index.html

40 of 45

Apple

Banana

41 of 45

<p>Birthday month is {{ bday.month }}</p>

render_template("index.html",

bday={"month": "Feb", "day": 24})

app.py

index.html

42 of 45

Birthday month is Feb

43 of 45

{% ... %}

{{ ... }}

Jinja expressions (e.g., for)

Jinja variables (e.g., bday, message)

44 of 45

Render birthdays

In app.py and index.html, query for birthdays and display them in a table.

  • Use db.execute to query for a list of birthdays in the birthdays table.
  • Use Jinja syntax to print out birthdays one by one in a table.

45 of 45

This is CS50 �Week 9