Intro to MVC and Flask RESTful
CSCI 344: Advanced Web Technologies
Spring 2023
Announcements
The “Model-View-Controller” (MVC) Design Pattern
MVC is an architectural pattern for implementing user interfaces. Divides an application into three interconnected parts: the Model, the View, and the Controller.
The “Model-View-Controller” (MVC) Design Pattern
There aren’t always clear lines between the “view” and the “controller”
For instance, in our Flask RESTful app, much of the logic that is typically put in the “controller” is actually situated in the “views” folder.
What is Flask RESTful?
Code Walkthrough
Let’s walk through how Flask, Flask RESTful, and SQLAlchemy can work together to create a REST API for Photo App. Specifically, we’ll talk about:
Models (see models folder)
post = Post.query.get(5)
print(post.user.username) # does the join for you
print(post.comments) # does the join for you
Views (see views folder)
In the context of Flask RESTful, views are organized using the Resource class.
app.py
app.py is the entry point to our app. All traffic is first routed to app.py and then forwarded on to the appropriate Resource instance. Things to note:
Tests (see tests folder)
Sarah has created a bunch of tests that are verifying that you have implemented your queries correctly (i.e. that the user can only see data they’re supposed to see and that they can’t modify resources they’re not allowed to modify.
Update:
Tips for Using Automated Tests
If time: Start Tutorial 11
Building your first REST API endpoint
Tips for Organizing Code