Flask/Sql Alchemy
Model-View-Controller Pattern in Flask
MVC Overview: Software design pattern dividing applications into:
Model: Manages data, logic, rules, and often data storage.
View: Presents information to users with HTML/CSS and collects user input.
Controller: Accepts user input and interacts with model/view.
Flask Framework
Flask: Minimalistic Python framework for web applications, supports extensions.
ORM Library: Preferred ORM is SQLAlchemy for CRUD operations.
Model Component in Flask
Model Declaration: Defined using classes, with attributes mapped to database columns.
CRUD Operations: Involve creating, querying, updating, and deleting objects in the database.
View Component in Flask
Views: Built using templates. Jinja is commonly used for templating in Flask.
Template Structure: Supports embedding variables, looping, and conditionals.
Controller Component in Flask
Controller Functions: Defined using functions or methods, handle HTTP requests and responses.
Routing: Uses decorators to map HTTP requests to controller functions.
Data Handling in HTML Forms
Form Submission: Values encapsulated in HTTP request as name-value pairs upon form submission.
Reading Request Parameters: Utilizing
requestobject in controller functions to access form data.
Using SQLAlchemy in Flask
Defining Columns: Use Mapped attributes to declare types, nullability, and roles of columns.
Relationships: Defined for one-to-many relationships through foreign keys and relationships attributes.
Querying in SQLAlchemy
Building Queries: Involves selecting classes, applying conditions, and sorting results.
Running Queries: Use session methods to execute queries and retrieve data.
Jinja Template Fundamentals
Expressions and Logic: Use
{{ }}for expressions,{% %}for control flow (loops/conditionals).Template Inheritance: Base templates define structure; specific templates can extend them.
Template Inclusion: Reusable template files can be included within other templates.
Relative Paths
URL Structure: Relative paths streamline referencing resources, enhancing maintainability and portability.