2025-04-10 15:52:02 -06:00
<!DOCTYPE html>
< html lang = "en" data-content_root = "../" >
< head >
< meta charset = "utf-8" / >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" / >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
2025-04-11 03:04:22 +00:00
< title > Patterns for Flask — Flask Documentation (3.2.x)< / title >
2025-04-10 15:52:02 -06:00
< link rel = "stylesheet" type = "text/css" href = "../_static/pygments.css?v=6625fa76" / >
< link rel = "stylesheet" type = "text/css" href = "../_static/flask.css?v=b87c8d14" / >
< script src = "../_static/documentation_options.js?v=56528222" > < / script >
< script src = "../_static/doctools.js?v=9bcbadda" > < / script >
< script src = "../_static/sphinx_highlight.js?v=dc90522c" > < / script >
< script data-project = "flask" data-version = "3.2.x" src = "../_static/describe_version.js?v=fa7f30d0" > < / script >
< link rel = "icon" href = "../_static/shortcut-icon.png" / >
< link rel = "index" title = "Index" href = "../genindex.html" / >
< link rel = "search" title = "Search" href = "../search.html" / >
2025-04-11 03:04:22 +00:00
< link rel = "next" title = "Large Applications as Packages" href = "packages.html" / >
< link rel = "prev" title = "Working with the Shell" href = "../shell.html" / >
2025-04-10 15:52:02 -06:00
< / head > < body >
< div class = "related" role = "navigation" aria-label = "Related" >
< h3 > Navigation< / h3 >
< ul >
< li class = "right" style = "margin-right: 10px" >
< a href = "../genindex.html" title = "General Index"
accesskey="I">index< / a > < / li >
< li class = "right" >
< a href = "../py-modindex.html" title = "Python Module Index"
>modules< / a > |< / li >
< li class = "right" >
2025-04-11 03:04:22 +00:00
< a href = "packages.html" title = "Large Applications as Packages"
2025-04-10 15:52:02 -06:00
accesskey="N">next< / a > |< / li >
< li class = "right" >
2025-04-11 03:04:22 +00:00
< a href = "../shell.html" title = "Working with the Shell"
2025-04-10 15:52:02 -06:00
accesskey="P">previous< / a > |< / li >
< li class = "nav-item nav-item-0" > < a href = "../index.html" > Flask Documentation (3.2.x)< / a > » < / li >
2025-04-11 03:04:22 +00:00
< li class = "nav-item nav-item-this" > < a href = "" > Patterns for Flask< / a > < / li >
2025-04-10 15:52:02 -06:00
< / ul >
2025-04-11 03:04:22 +00:00
< / div >
2025-04-10 15:52:02 -06:00
< div class = "document" >
< div class = "documentwrapper" >
< div class = "bodywrapper" >
< div class = "body" role = "main" >
2025-04-11 03:04:22 +00:00
< section id = "patterns-for-flask" >
< h1 > Patterns for Flask< a class = "headerlink" href = "#patterns-for-flask" title = "Link to this heading" > ¶< / a > < / h1 >
< p > Certain features and interactions are common enough that you will find
them in most web applications. For example, many applications use a
relational database and user authentication. They will open a database
connection at the beginning of the request and get the information for
the logged in user. At the end of the request, the database connection
is closed.< / p >
< p > These types of patterns may be a bit outside the scope of Flask itself,
but Flask makes it easy to implement them. Some common patterns are
collected in the following pages.< / p >
< div class = "toctree-wrapper compound" >
< ul >
< li class = "toctree-l1" > < a class = "reference internal" href = "packages.html" > Large Applications as Packages< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "packages.html#simple-packages" > Simple Packages< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "packages.html#working-with-blueprints" > Working with Blueprints< / a > < / li >
2025-04-10 15:52:02 -06:00
< / ul >
2025-04-11 03:04:22 +00:00
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "appfactories.html" > Application Factories< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "appfactories.html#basic-factories" > Basic Factories< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "appfactories.html#factories-extensions" > Factories & Extensions< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "appfactories.html#using-applications" > Using Applications< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "appfactories.html#factory-improvements" > Factory Improvements< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "appdispatch.html" > Application Dispatching< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "appdispatch.html#working-with-this-document" > Working with this Document< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "appdispatch.html#combining-applications" > Combining Applications< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "appdispatch.html#dispatch-by-subdomain" > Dispatch by Subdomain< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "appdispatch.html#dispatch-by-path" > Dispatch by Path< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "urlprocessors.html" > Using URL Processors< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "urlprocessors.html#internationalized-application-urls" > Internationalized Application URLs< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "urlprocessors.html#internationalized-blueprint-urls" > Internationalized Blueprint URLs< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "sqlite3.html" > Using SQLite 3 with Flask< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "sqlite3.html#connect-on-demand" > Connect on Demand< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "sqlite3.html#easy-querying" > Easy Querying< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "sqlite3.html#initial-schemas" > Initial Schemas< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "sqlalchemy.html" > SQLAlchemy in Flask< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "sqlalchemy.html#flask-sqlalchemy-extension" > Flask-SQLAlchemy Extension< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "sqlalchemy.html#declarative" > Declarative< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "sqlalchemy.html#manual-object-relational-mapping" > Manual Object Relational Mapping< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "sqlalchemy.html#sql-abstraction-layer" > SQL Abstraction Layer< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "fileuploads.html" > Uploading Files< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "fileuploads.html#a-gentle-introduction" > A Gentle Introduction< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "fileuploads.html#improving-uploads" > Improving Uploads< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "fileuploads.html#upload-progress-bars" > Upload Progress Bars< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "fileuploads.html#an-easier-solution" > An Easier Solution< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "caching.html" > Caching< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "viewdecorators.html" > View Decorators< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "viewdecorators.html#login-required-decorator" > Login Required Decorator< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "viewdecorators.html#caching-decorator" > Caching Decorator< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "viewdecorators.html#templating-decorator" > Templating Decorator< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "viewdecorators.html#endpoint-decorator" > Endpoint Decorator< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "wtforms.html" > Form Validation with WTForms< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "wtforms.html#the-forms" > The Forms< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "wtforms.html#in-the-view" > In the View< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "wtforms.html#forms-in-templates" > Forms in Templates< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "templateinheritance.html" > Template Inheritance< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "templateinheritance.html#base-template" > Base Template< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "templateinheritance.html#child-template" > Child Template< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "flashing.html" > Message Flashing< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "flashing.html#simple-flashing" > Simple Flashing< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "flashing.html#flashing-with-categories" > Flashing With Categories< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "flashing.html#filtering-flash-messages" > Filtering Flash Messages< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "javascript.html" > JavaScript, < code class = "docutils literal notranslate" > < span class = "pre" > fetch< / span > < / code > , and JSON< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "javascript.html#rendering-templates" > Rendering Templates< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "javascript.html#generating-urls" > Generating URLs< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "javascript.html#making-a-request-with-fetch" > Making a Request with < code class = "docutils literal notranslate" > < span class = "pre" > fetch< / span > < / code > < / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "javascript.html#following-redirects" > Following Redirects< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "javascript.html#replacing-content" > Replacing Content< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "javascript.html#return-json-from-views" > Return JSON from Views< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "javascript.html#receiving-json-in-views" > Receiving JSON in Views< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "lazyloading.html" > Lazily Loading Views< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "lazyloading.html#converting-to-centralized-url-map" > Converting to Centralized URL Map< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "lazyloading.html#loading-late" > Loading Late< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "mongoengine.html" > MongoDB with MongoEngine< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "mongoengine.html#configuration" > Configuration< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "mongoengine.html#mapping-documents" > Mapping Documents< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "mongoengine.html#creating-data" > Creating Data< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "mongoengine.html#queries" > Queries< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "mongoengine.html#documentation" > Documentation< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "favicon.html" > Adding a favicon< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "favicon.html#see-also" > See also< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "streaming.html" > Streaming Contents< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "streaming.html#basic-usage" > Basic Usage< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "streaming.html#streaming-from-templates" > Streaming from Templates< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "streaming.html#streaming-with-context" > Streaming with Context< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "deferredcallbacks.html" > Deferred Request Callbacks< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "methodoverrides.html" > Adding HTTP Method Overrides< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "requestchecksum.html" > Request Content Checksums< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "celery.html" > Background Tasks with Celery< / a > < ul >
< li class = "toctree-l2" > < a class = "reference internal" href = "celery.html#install" > Install< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "celery.html#integrate-celery-with-flask" > Integrate Celery with Flask< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "celery.html#application-factory" > Application Factory< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "celery.html#defining-tasks" > Defining Tasks< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "celery.html#calling-tasks" > Calling Tasks< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "celery.html#getting-results" > Getting Results< / a > < / li >
< li class = "toctree-l2" > < a class = "reference internal" href = "celery.html#passing-data-to-tasks" > Passing Data to Tasks< / a > < / li >
< / ul >
< / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "subclassing.html" > Subclassing Flask< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "singlepageapplications.html" > Single-Page Applications< / a > < / li >
< / ul >
< / div >
2025-04-10 15:52:02 -06:00
< / section >
< div class = "clearer" > < / div >
< / div >
< / div >
< / div >
< span id = "sidebar-top" > < / span >
< div class = "sphinxsidebar" role = "navigation" aria-label = "Main" >
< div class = "sphinxsidebarwrapper" >
2025-04-11 03:04:22 +00:00
2025-04-10 15:52:02 -06:00
< p class = "logo" > < a href = "../index.html" >
< img class = "logo" src = "../_static/flask-vertical.png" alt = "Logo of Flask" / >
< / a > < / p >
< h3 > Navigation< / h3 >
< ul >
< li > < a href = "../index.html" > Overview< / a >
< ul >
2025-04-11 03:04:22 +00:00
< li > Previous: < a href = "../shell.html" title = "previous chapter" > Working with the Shell< / a >
< li > Next: < a href = "packages.html" title = "next chapter" > Large Applications as Packages< / a >
2025-04-10 15:52:02 -06:00
< / ul >
< / li >
< / ul >
< search id = "searchbox" style = "display: none" role = "search" >
< h3 id = "searchlabel" > Quick search< / h3 >
< div class = "searchformwrapper" >
< form class = "search" action = "../search.html" method = "get" >
< input type = "text" name = "q" aria-labelledby = "searchlabel" autocomplete = "off" autocorrect = "off" autocapitalize = "off" spellcheck = "false" / >
< input type = "submit" value = "Go" / >
< / form >
< / div >
< / search >
< script > document . getElementById ( 'searchbox' ) . style . display = "block" < / script > < div id = "ethical-ad-placement" > < / div >
< / div >
< / div >
< div class = "clearer" > < / div >
< / div >
< div class = "footer" role = "contentinfo" >
© Copyright 2010 Pallets.
Created using < a href = "https://www.sphinx-doc.org/" > Sphinx< / a > 8.1.3.
< / div >
< / body >
2025-04-11 03:04:22 +00:00
< / html >