flask/_build/patterns/subclassing/index.html

110 lines
5.6 KiB
HTML
Raw Normal View History

2025-03-24 20:10:13 -07: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">
<title>Subclassing Flask &#8212; Flask Documentation (3.1.x)</title>
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=4f649999" />
<link rel="stylesheet" type="text/css" href="../../_static/flask.css?v=b87c8d14" />
<script src="../../_static/documentation_options.js?v=d71c4578"></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.1.x" src="../../_static/describe_version.js?v=fa7f30d0"></script>
<link rel="icon" href="../../_static/shortcut-icon.png"/>
<link rel="index" title="Index" href="../../genindex/" />
<link rel="search" title="Search" href="../../search/" />
<link rel="next" title="Single-Page Applications" href="../singlepageapplications/" />
<link rel="prev" title="Background Tasks with Celery" href="../celery/" />
</head><body>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../genindex/" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../py-modindex/" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="../singlepageapplications/" title="Single-Page Applications"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="../celery/" title="Background Tasks with Celery"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../../">Flask Documentation (3.1.x)</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../" accesskey="U">Patterns for Flask</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">Subclassing Flask</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="subclassing-flask">
<h1>Subclassing Flask<a class="headerlink" href="#subclassing-flask" title="Link to this heading"></a></h1>
<p>The <a class="reference internal" href="../../api/#flask.Flask" title="flask.Flask"><code class="xref py py-class docutils literal notranslate"><span class="pre">Flask</span></code></a> class is designed for subclassing.</p>
<p>For example, you may want to override how request parameters are handled to preserve their order:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">Request</span>
<span class="kn">from</span> <span class="nn">werkzeug.datastructures</span> <span class="kn">import</span> <span class="n">ImmutableOrderedMultiDict</span>
<span class="k">class</span> <span class="nc">MyRequest</span><span class="p">(</span><span class="n">Request</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Request subclass to override request parameter storage&quot;&quot;&quot;</span>
<span class="n">parameter_storage_class</span> <span class="o">=</span> <span class="n">ImmutableOrderedMultiDict</span>
<span class="k">class</span> <span class="nc">MyFlask</span><span class="p">(</span><span class="n">Flask</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Flask subclass using the custom request class&quot;&quot;&quot;</span>
<span class="n">request_class</span> <span class="o">=</span> <span class="n">MyRequest</span>
</pre></div>
</div>
<p>This is the recommended approach for overriding or augmenting Flasks internal functionality.</p>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<span id="sidebar-top"></span>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../../">
<img class="logo" src="../../_static/flask-vertical.png" alt="Logo of Flask"/>
</a></p>
<h3>Navigation</h3>
<ul>
<li><a href="../../">Overview</a>
<ul>
<li><a href="../">Patterns for Flask</a>
<ul>
<li>Previous: <a href="../celery/" title="previous chapter">Background Tasks with Celery</a>
<li>Next: <a href="../singlepageapplications/" title="next chapter">Single-Page Applications</a></ul>
</li>
</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/" 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">
&#169; Copyright 2010 Pallets.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3.
</div>
</body>
</html>