Catalog / Flask Cheatsheet
Flask Cheatsheet
A quick reference guide to the Flask micro web framework for Python, covering essential concepts, code snippets, and best practices.
Core Concepts
Basic Application Structure
This is the basic structure of a Flask application. |
The |
Routing
|
Binds the function to the |
|
Adds a variable part to the URL. The variable is passed as an argument to the function. |
|
Specifies the type of the variable as an integer. Other options include |
|
Specifies the HTTP methods allowed for the route. Defaults to |
Request Object
The |
|
|
Templates and Rendering
Rendering Templates
The |
Templates are located in the |
Jinja2 Basics
|
Outputs the value of a variable. |
|
Executes a statement (e.g., loop, conditional). |
|
Comment. |
|
Generates a URL for a function based on its route. Useful for avoiding hardcoded URLs. |
Template Inheritance
|
Base template (
|
Child template (
|
Working with Databases
Flask-SQLAlchemy
Flask-SQLAlchemy simplifies using SQLAlchemy with Flask.
|
Configuration: |
Defining Models
|
Defines an integer primary key column. |
|
Defines a string column with a maximum length of 80 characters that must be unique. |
|
Defines a relationship between two models. |
Database Operations
|
|
|
Forms and Validation
Flask-WTF
Flask-WTF integrates WTForms with Flask for handling forms.
|
Define forms as classes inheriting from |
Using Forms in Views
|
|
Displaying Forms in Templates
|
Use |