Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 65530c6176058f9b54858c3b4f6385e6 > files > 768

python-django-doc-1.8.19-1.mga6.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" lang="">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Middleware &#8212; Django 1.8.19 documentation</title>
    
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.8.19',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="top" title="Django 1.8.19 documentation" href="../contents.html" />
    <link rel="up" title="API Reference" href="index.html" />
    <link rel="next" title="Migration Operations" href="migration-operations.html" />
    <link rel="prev" title="Form and field validation" href="forms/validation.html" />



 
<script type="text/javascript" src="../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body role="document">

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 1.8.19 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="forms/validation.html" title="Form and field validation">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="migration-operations.html" title="Migration Operations">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-middleware">
            
  <div class="section" id="s-module-django.middleware">
<span id="s-middleware"></span><span id="module-django.middleware"></span><span id="middleware"></span><h1>Middleware<a class="headerlink" href="#module-django.middleware" title="Permalink to this headline">¶</a></h1>
<p>This document explains all middleware components that come with Django. For
information on how to use them and how to write your own middleware, see
the <a class="reference internal" href="../topics/http/middleware.html"><span class="doc">middleware usage guide</span></a>.</p>
<div class="section" id="s-available-middleware">
<span id="available-middleware"></span><h2>Available middleware<a class="headerlink" href="#available-middleware" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-module-django.middleware.cache">
<span id="s-cache-middleware"></span><span id="module-django.middleware.cache"></span><span id="cache-middleware"></span><h3>Cache middleware<a class="headerlink" href="#module-django.middleware.cache" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.middleware.cache.UpdateCacheMiddleware">
<em class="property">class </em><code class="descname">UpdateCacheMiddleware</code><a class="headerlink" href="#django.middleware.cache.UpdateCacheMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.middleware.cache.FetchFromCacheMiddleware">
<em class="property">class </em><code class="descname">FetchFromCacheMiddleware</code><a class="headerlink" href="#django.middleware.cache.FetchFromCacheMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Enable the site-wide cache. If these are enabled, each Django-powered page will
be cached for as long as the <a class="reference internal" href="settings.html#std:setting-CACHE_MIDDLEWARE_SECONDS"><code class="xref std std-setting docutils literal"><span class="pre">CACHE_MIDDLEWARE_SECONDS</span></code></a> setting
defines. See the <a class="reference internal" href="../topics/cache.html"><span class="doc">cache documentation</span></a>.</p>
</div>
<div class="section" id="s-module-django.middleware.common">
<span id="s-common-middleware"></span><span id="module-django.middleware.common"></span><span id="common-middleware"></span><h3>&#8220;Common&#8221; middleware<a class="headerlink" href="#module-django.middleware.common" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.middleware.common.CommonMiddleware">
<em class="property">class </em><code class="descname">CommonMiddleware</code><a class="reference internal" href="../_modules/django/middleware/common.html#CommonMiddleware"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.middleware.common.CommonMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Adds a few conveniences for perfectionists:</p>
<ul>
<li><p class="first">Forbids access to user agents in the <a class="reference internal" href="settings.html#std:setting-DISALLOWED_USER_AGENTS"><code class="xref std std-setting docutils literal"><span class="pre">DISALLOWED_USER_AGENTS</span></code></a>
setting, which should be a list of compiled regular expression objects.</p>
</li>
<li><p class="first">Performs URL rewriting based on the <a class="reference internal" href="settings.html#std:setting-APPEND_SLASH"><code class="xref std std-setting docutils literal"><span class="pre">APPEND_SLASH</span></code></a> and
<a class="reference internal" href="settings.html#std:setting-PREPEND_WWW"><code class="xref std std-setting docutils literal"><span class="pre">PREPEND_WWW</span></code></a> settings.</p>
<p>If <a class="reference internal" href="settings.html#std:setting-APPEND_SLASH"><code class="xref std std-setting docutils literal"><span class="pre">APPEND_SLASH</span></code></a> is <code class="docutils literal"><span class="pre">True</span></code> and the initial URL doesn&#8217;t end
with a slash, and it is not found in the URLconf, then a new URL is
formed by appending a slash at the end. If this new URL is found in the
URLconf, then Django redirects the request to this new URL. Otherwise,
the initial URL is processed as usual.</p>
<p>For example, <code class="docutils literal"><span class="pre">foo.com/bar</span></code> will be redirected to <code class="docutils literal"><span class="pre">foo.com/bar/</span></code> if
you don&#8217;t have a valid URL pattern for <code class="docutils literal"><span class="pre">foo.com/bar</span></code> but <em>do</em> have a
valid pattern for <code class="docutils literal"><span class="pre">foo.com/bar/</span></code>.</p>
<p>If <a class="reference internal" href="settings.html#std:setting-PREPEND_WWW"><code class="xref std std-setting docutils literal"><span class="pre">PREPEND_WWW</span></code></a> is <code class="docutils literal"><span class="pre">True</span></code>, URLs that lack a leading &#8220;www.&#8221;
will be redirected to the same URL with a leading &#8220;www.&#8221;</p>
<p>Both of these options are meant to normalize URLs. The philosophy is that
each URL should exist in one, and only one, place. Technically a URL
<code class="docutils literal"><span class="pre">foo.com/bar</span></code> is distinct from <code class="docutils literal"><span class="pre">foo.com/bar/</span></code> &#8211; a search-engine
indexer would treat them as separate URLs &#8211; so it&#8217;s best practice to
normalize URLs.</p>
</li>
<li><p class="first">Handles ETags based on the <a class="reference internal" href="settings.html#std:setting-USE_ETAGS"><code class="xref std std-setting docutils literal"><span class="pre">USE_ETAGS</span></code></a> setting. If
<a class="reference internal" href="settings.html#std:setting-USE_ETAGS"><code class="xref std std-setting docutils literal"><span class="pre">USE_ETAGS</span></code></a> is set to <code class="docutils literal"><span class="pre">True</span></code>, Django will calculate an ETag
for each request by MD5-hashing the page content, and it&#8217;ll take care of
sending <code class="docutils literal"><span class="pre">Not</span> <span class="pre">Modified</span></code> responses, if appropriate.</p>
</li>
</ul>
<dl class="attribute">
<dt id="django.middleware.common.CommonMiddleware.response_redirect_class">
<code class="descclassname">CommonMiddleware.</code><code class="descname">response_redirect_class</code><a class="headerlink" href="#django.middleware.common.CommonMiddleware.response_redirect_class" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="versionadded">
<span class="title">New in Django 1.8.</span> </div>
<p>Defaults to <a class="reference internal" href="request-response.html#django.http.HttpResponsePermanentRedirect" title="django.http.HttpResponsePermanentRedirect"><code class="xref py py-class docutils literal"><span class="pre">HttpResponsePermanentRedirect</span></code></a>. Subclass
<code class="docutils literal"><span class="pre">CommonMiddleware</span></code> and override the attribute to customize the redirects
issued by the middleware.</p>
<dl class="class">
<dt id="django.middleware.common.BrokenLinkEmailsMiddleware">
<em class="property">class </em><code class="descname">BrokenLinkEmailsMiddleware</code><a class="reference internal" href="../_modules/django/middleware/common.html#BrokenLinkEmailsMiddleware"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.middleware.common.BrokenLinkEmailsMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<ul class="simple">
<li>Sends broken link notification emails to <a class="reference internal" href="settings.html#std:setting-MANAGERS"><code class="xref std std-setting docutils literal"><span class="pre">MANAGERS</span></code></a> (see
<a class="reference internal" href="../howto/error-reporting.html"><span class="doc">Error reporting</span></a>).</li>
</ul>
</div>
<div class="section" id="s-module-django.middleware.gzip">
<span id="s-gzip-middleware"></span><span id="module-django.middleware.gzip"></span><span id="gzip-middleware"></span><h3>GZip middleware<a class="headerlink" href="#module-django.middleware.gzip" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.middleware.gzip.GZipMiddleware">
<em class="property">class </em><code class="descname">GZipMiddleware</code><a class="headerlink" href="#django.middleware.gzip.GZipMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Security researchers recently revealed that when compression techniques
(including <code class="docutils literal"><span class="pre">GZipMiddleware</span></code>) are used on a website, the site becomes
exposed to a number of possible attacks. These approaches can be used to
compromise, among other things, Django&#8217;s CSRF protection. Before using
<code class="docutils literal"><span class="pre">GZipMiddleware</span></code> on your site, you should consider very carefully whether
you are subject to these attacks. If you&#8217;re in <em>any</em> doubt about whether
you&#8217;re affected, you should avoid using <code class="docutils literal"><span class="pre">GZipMiddleware</span></code>. For more
details, see the <a class="reference external" href="http://breachattack.com/resources/BREACH%20-%20SSL,%20gone%20in%2030%20seconds.pdf">the BREACH paper (PDF)</a> and <a class="reference external" href="http://breachattack.com">breachattack.com</a>.</p>
</div>
<p>Compresses content for browsers that understand GZip compression (all modern
browsers).</p>
<p>This middleware should be placed before any other middleware that need to
read or write the response body so that compression happens afterward.</p>
<p>It will NOT compress content if any of the following are true:</p>
<ul class="simple">
<li>The content body is less than 200 bytes long.</li>
<li>The response has already set the <code class="docutils literal"><span class="pre">Content-Encoding</span></code> header.</li>
<li>The request (the browser) hasn&#8217;t sent an <code class="docutils literal"><span class="pre">Accept-Encoding</span></code> header
containing <code class="docutils literal"><span class="pre">gzip</span></code>.</li>
</ul>
<p>You can apply GZip compression to individual views using the
<a class="reference internal" href="../topics/http/decorators.html#django.views.decorators.gzip.gzip_page" title="django.views.decorators.gzip.gzip_page"><code class="xref py py-func docutils literal"><span class="pre">gzip_page()</span></code></a> decorator.</p>
</div>
<div class="section" id="s-module-django.middleware.http">
<span id="s-conditional-get-middleware"></span><span id="module-django.middleware.http"></span><span id="conditional-get-middleware"></span><h3>Conditional GET middleware<a class="headerlink" href="#module-django.middleware.http" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.middleware.http.ConditionalGetMiddleware">
<em class="property">class </em><code class="descname">ConditionalGetMiddleware</code><a class="reference internal" href="../_modules/django/middleware/http.html#ConditionalGetMiddleware"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.middleware.http.ConditionalGetMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Handles conditional GET operations. If the response has a <code class="docutils literal"><span class="pre">ETag</span></code> or
<code class="docutils literal"><span class="pre">Last-Modified</span></code> header, and the request has <code class="docutils literal"><span class="pre">If-None-Match</span></code> or
<code class="docutils literal"><span class="pre">If-Modified-Since</span></code>, the response is replaced by an
<a class="reference internal" href="request-response.html#django.http.HttpResponseNotModified" title="django.http.HttpResponseNotModified"><code class="xref py py-class docutils literal"><span class="pre">HttpResponseNotModified</span></code></a>.</p>
<p>Also sets the <code class="docutils literal"><span class="pre">Date</span></code> and <code class="docutils literal"><span class="pre">Content-Length</span></code> response-headers.</p>
</div>
<div class="section" id="s-module-django.middleware.locale">
<span id="s-locale-middleware"></span><span id="module-django.middleware.locale"></span><span id="locale-middleware"></span><h3>Locale middleware<a class="headerlink" href="#module-django.middleware.locale" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.middleware.locale.LocaleMiddleware">
<em class="property">class </em><code class="descname">LocaleMiddleware</code><a class="headerlink" href="#django.middleware.locale.LocaleMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Enables language selection based on data from the request. It customizes
content for each user. See the <a class="reference internal" href="../topics/i18n/translation.html"><span class="doc">internationalization documentation</span></a>.</p>
<dl class="attribute">
<dt id="django.middleware.locale.LocaleMiddleware.response_redirect_class">
<code class="descclassname">LocaleMiddleware.</code><code class="descname">response_redirect_class</code><a class="headerlink" href="#django.middleware.locale.LocaleMiddleware.response_redirect_class" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Defaults to <a class="reference internal" href="request-response.html#django.http.HttpResponseRedirect" title="django.http.HttpResponseRedirect"><code class="xref py py-class docutils literal"><span class="pre">HttpResponseRedirect</span></code></a>. Subclass
<code class="docutils literal"><span class="pre">LocaleMiddleware</span></code> and override the attribute to customize the redirects
issued by the middleware.</p>
</div>
<div class="section" id="s-module-django.contrib.messages.middleware">
<span id="s-message-middleware"></span><span id="module-django.contrib.messages.middleware"></span><span id="message-middleware"></span><h3>Message middleware<a class="headerlink" href="#module-django.contrib.messages.middleware" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.contrib.messages.middleware.MessageMiddleware">
<em class="property">class </em><code class="descname">MessageMiddleware</code><a class="reference internal" href="../_modules/django/contrib/messages/middleware.html#MessageMiddleware"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.messages.middleware.MessageMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Enables cookie- and session-based message support. See the
<a class="reference internal" href="contrib/messages.html"><span class="doc">messages documentation</span></a>.</p>
</div>
<div class="section" id="s-module-django.middleware.security">
<span id="s-id1"></span><span id="s-security-middleware"></span><span id="module-django.middleware.security"></span><span id="id1"></span><span id="security-middleware"></span><h3>Security middleware<a class="headerlink" href="#module-django.middleware.security" title="Permalink to this headline">¶</a></h3>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">If your deployment situation allows, it&#8217;s usually a good idea to have your
front-end Web server perform the functionality provided by the
<code class="docutils literal"><span class="pre">SecurityMiddleware</span></code>. That way, if there are requests that aren&#8217;t served
by Django (such as static media or user-uploaded files), they will have
the same protections as requests to your Django application.</p>
</div>
<dl class="class">
<dt id="django.middleware.security.SecurityMiddleware">
<em class="property">class </em><code class="descname">SecurityMiddleware</code><a class="reference internal" href="../_modules/django/middleware/security.html#SecurityMiddleware"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.middleware.security.SecurityMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="versionadded">
<span class="title">New in Django 1.8.</span> </div>
<p>The <code class="docutils literal"><span class="pre">django.middleware.security.SecurityMiddleware</span></code> provides several security
enhancements to the request/response cycle. Each one can be independently
enabled or disabled with a setting.</p>
<ul class="simple">
<li><a class="reference internal" href="settings.html#std:setting-SECURE_BROWSER_XSS_FILTER"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_BROWSER_XSS_FILTER</span></code></a></li>
<li><a class="reference internal" href="settings.html#std:setting-SECURE_CONTENT_TYPE_NOSNIFF"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_CONTENT_TYPE_NOSNIFF</span></code></a></li>
<li><a class="reference internal" href="settings.html#std:setting-SECURE_HSTS_INCLUDE_SUBDOMAINS"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_HSTS_INCLUDE_SUBDOMAINS</span></code></a></li>
<li><a class="reference internal" href="settings.html#std:setting-SECURE_HSTS_SECONDS"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_HSTS_SECONDS</span></code></a></li>
<li><a class="reference internal" href="settings.html#std:setting-SECURE_REDIRECT_EXEMPT"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_REDIRECT_EXEMPT</span></code></a></li>
<li><a class="reference internal" href="settings.html#std:setting-SECURE_SSL_HOST"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_SSL_HOST</span></code></a></li>
<li><a class="reference internal" href="settings.html#std:setting-SECURE_SSL_REDIRECT"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_SSL_REDIRECT</span></code></a></li>
</ul>
<div class="section" id="s-http-strict-transport-security">
<span id="s-id2"></span><span id="http-strict-transport-security"></span><span id="id2"></span><h4>HTTP Strict Transport Security<a class="headerlink" href="#http-strict-transport-security" title="Permalink to this headline">¶</a></h4>
<p>For sites that should only be accessed over HTTPS, you can instruct modern
browsers to refuse to connect to your domain name via an insecure connection
(for a given period of time) by setting the <a class="reference external" href="https://en.wikipedia.org/wiki/Strict_Transport_Security">&#8220;Strict-Transport-Security&#8221;
header</a>. This reduces your exposure to some SSL-stripping man-in-the-middle
(MITM) attacks.</p>
<p><code class="docutils literal"><span class="pre">SecurityMiddleware</span></code> will set this header for you on all HTTPS responses if
you set the <a class="reference internal" href="settings.html#std:setting-SECURE_HSTS_SECONDS"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_HSTS_SECONDS</span></code></a> setting to a non-zero integer value.</p>
<p>When enabling HSTS, it&#8217;s a good idea to first use a small value for testing,
for example, <a class="reference internal" href="settings.html#std:setting-SECURE_HSTS_SECONDS"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_HSTS_SECONDS</span> <span class="pre">=</span> <span class="pre">3600</span></code></a> for one
hour. Each time a Web browser sees the HSTS header from your site, it will
refuse to communicate non-securely (using HTTP) with your domain for the given
period of time. Once you confirm that all assets are served securely on your
site (i.e. HSTS didn&#8217;t break anything), it&#8217;s a good idea to increase this value
so that infrequent visitors will be protected (31536000 seconds, i.e. 1 year,
is common).</p>
<p>Additionally, if you set the <a class="reference internal" href="settings.html#std:setting-SECURE_HSTS_INCLUDE_SUBDOMAINS"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_HSTS_INCLUDE_SUBDOMAINS</span></code></a> setting
to <code class="docutils literal"><span class="pre">True</span></code>, <code class="docutils literal"><span class="pre">SecurityMiddleware</span></code> will add the <code class="docutils literal"><span class="pre">includeSubDomains</span></code> tag to
the <code class="docutils literal"><span class="pre">Strict-Transport-Security</span></code> header. This is recommended (assuming all
subdomains are served exclusively using HTTPS), otherwise your site may still
be vulnerable via an insecure connection to a subdomain.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>The HSTS policy applies to your entire domain, not just the URL of the
response that you set the header on. Therefore, you should only use it if
your entire domain is served via HTTPS only.</p>
<p class="last">Browsers properly respecting the HSTS header will refuse to allow users to
bypass warnings and connect to a site with an expired, self-signed, or
otherwise invalid SSL certificate. If you use HSTS, make sure your
certificates are in good shape and stay that way!</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you are deployed behind a load-balancer or reverse-proxy server, and the
<code class="docutils literal"><span class="pre">Strict-Transport-Security</span></code> header is not being added to your responses,
it may be because Django doesn&#8217;t realize that it&#8217;s on a secure connection;
you may need to set the <a class="reference internal" href="settings.html#std:setting-SECURE_PROXY_SSL_HEADER"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_PROXY_SSL_HEADER</span></code></a> setting.</p>
</div>
</div>
<div class="section" id="s-x-content-type-options-nosniff">
<span id="s-x-content-type-options"></span><span id="x-content-type-options-nosniff"></span><span id="x-content-type-options"></span><h4><code class="docutils literal"><span class="pre">X-Content-Type-Options:</span> <span class="pre">nosniff</span></code><a class="headerlink" href="#x-content-type-options-nosniff" title="Permalink to this headline">¶</a></h4>
<p>Some browsers will try to guess the content types of the assets that they
fetch, overriding the <code class="docutils literal"><span class="pre">Content-Type</span></code> header. While this can help display
sites with improperly configured servers, it can also pose a security
risk.</p>
<p>If your site serves user-uploaded files, a malicious user could upload a
specially-crafted file that would be interpreted as HTML or JavaScript by
the browser when you expected it to be something harmless.</p>
<p>To learn more about this header and how the browser treats it, you can
read about it on the <a class="reference external" href="http://blogs.msdn.com/b/ie/archive/2008/09/02/ie8-security-part-vi-beta-2-update.aspx">IE Security Blog</a>.</p>
<p>To prevent the browser from guessing the content type and force it to
always use the type provided in the <code class="docutils literal"><span class="pre">Content-Type</span></code> header, you can pass
the <code class="docutils literal"><span class="pre">X-Content-Type-Options:</span> <span class="pre">nosniff</span></code> header.  <code class="docutils literal"><span class="pre">SecurityMiddleware</span></code> will
do this for all responses if the <a class="reference internal" href="settings.html#std:setting-SECURE_CONTENT_TYPE_NOSNIFF"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_CONTENT_TYPE_NOSNIFF</span></code></a> setting
is <code class="docutils literal"><span class="pre">True</span></code>.</p>
<p>Note that in most deployment situations where Django isn&#8217;t involved in serving
user-uploaded files, this setting won&#8217;t help you. For example, if your
<a class="reference internal" href="settings.html#std:setting-MEDIA_URL"><code class="xref std std-setting docutils literal"><span class="pre">MEDIA_URL</span></code></a> is served directly by your front-end Web server (nginx,
Apache, etc.) then you&#8217;d want to set this header there. On the other hand, if
you are using Django to do something like require authorization in order to
download files and you cannot set the header using your Web server, this
setting will be useful.</p>
</div>
<div class="section" id="s-x-xss-protection-1-mode-block">
<span id="s-x-xss-protection"></span><span id="x-xss-protection-1-mode-block"></span><span id="x-xss-protection"></span><h4><code class="docutils literal"><span class="pre">X-XSS-Protection:</span> <span class="pre">1;</span> <span class="pre">mode=block</span></code><a class="headerlink" href="#x-xss-protection-1-mode-block" title="Permalink to this headline">¶</a></h4>
<p>Some browsers have the ability to block content that appears to be an <a class="reference external" href="https://en.wikipedia.org/wiki/Cross-site_scripting">XSS
attack</a>. They work by looking for JavaScript content in the GET or POST
parameters of a page. If the JavaScript is replayed in the server&#8217;s response,
the page is blocked from rendering and an error page is shown instead.</p>
<p>The <a class="reference external" href="http://blogs.msdn.com/b/ie/archive/2008/07/02/ie8-security-part-iv-the-xss-filter.aspx">X-XSS-Protection header</a> is used to control the operation of the
XSS filter.</p>
<p>To enable the XSS filter in the browser, and force it to always block
suspected XSS attacks, you can pass the <code class="docutils literal"><span class="pre">X-XSS-Protection:</span> <span class="pre">1;</span> <span class="pre">mode=block</span></code>
header. <code class="docutils literal"><span class="pre">SecurityMiddleware</span></code> will do this for all responses if the
<a class="reference internal" href="settings.html#std:setting-SECURE_BROWSER_XSS_FILTER"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_BROWSER_XSS_FILTER</span></code></a> setting is <code class="docutils literal"><span class="pre">True</span></code>.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">The browser XSS filter is a useful defense measure, but must not be
relied upon exclusively. It cannot detect all XSS attacks and not all
browsers support the header. Ensure you are still <a class="reference internal" href="../topics/security.html#cross-site-scripting"><span class="std std-ref">validating and
sanitizing</span></a> all input to prevent XSS attacks.</p>
</div>
</div>
<div class="section" id="s-ssl-redirect">
<span id="s-id3"></span><span id="ssl-redirect"></span><span id="id3"></span><h4>SSL Redirect<a class="headerlink" href="#ssl-redirect" title="Permalink to this headline">¶</a></h4>
<p>If your site offers both HTTP and HTTPS connections, most users will end up
with an unsecured connection by default. For best security, you should redirect
all HTTP connections to HTTPS.</p>
<p>If you set the <a class="reference internal" href="settings.html#std:setting-SECURE_SSL_REDIRECT"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_SSL_REDIRECT</span></code></a> setting to True,
<code class="docutils literal"><span class="pre">SecurityMiddleware</span></code> will permanently (HTTP 301) redirect all HTTP
connections to HTTPS.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For performance reasons, it&#8217;s preferable to do these redirects outside of
Django, in a front-end load balancer or reverse-proxy server such as
<a class="reference external" href="http://nginx.org">nginx</a>. <a class="reference internal" href="settings.html#std:setting-SECURE_SSL_REDIRECT"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_SSL_REDIRECT</span></code></a> is intended for the deployment
situations where this isn&#8217;t an option.</p>
</div>
<p>If the <a class="reference internal" href="settings.html#std:setting-SECURE_SSL_HOST"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_SSL_HOST</span></code></a> setting has a value, all redirects will be
sent to that host instead of the originally-requested host.</p>
<p>If there are a few pages on your site that should be available over HTTP, and
not redirected to HTTPS, you can list regular expressions to match those URLs
in the <a class="reference internal" href="settings.html#std:setting-SECURE_REDIRECT_EXEMPT"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_REDIRECT_EXEMPT</span></code></a> setting.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you are deployed behind a load-balancer or reverse-proxy server and
Django can&#8217;t seem to tell when a request actually is already secure, you
may need to set the <a class="reference internal" href="settings.html#std:setting-SECURE_PROXY_SSL_HEADER"><code class="xref std std-setting docutils literal"><span class="pre">SECURE_PROXY_SSL_HEADER</span></code></a> setting.</p>
</div>
</div>
</div>
<div class="section" id="s-module-django.contrib.sessions.middleware">
<span id="s-session-middleware"></span><span id="module-django.contrib.sessions.middleware"></span><span id="session-middleware"></span><h3>Session middleware<a class="headerlink" href="#module-django.contrib.sessions.middleware" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.contrib.sessions.middleware.SessionMiddleware">
<em class="property">class </em><code class="descname">SessionMiddleware</code><a class="headerlink" href="#django.contrib.sessions.middleware.SessionMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Enables session support. See the <a class="reference internal" href="../topics/http/sessions.html"><span class="doc">session documentation</span></a>.</p>
</div>
<div class="section" id="s-module-django.contrib.sites.middleware">
<span id="s-site-middleware"></span><span id="module-django.contrib.sites.middleware"></span><span id="site-middleware"></span><h3>Site middleware<a class="headerlink" href="#module-django.contrib.sites.middleware" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.contrib.sites.middleware.CurrentSiteMiddleware">
<em class="property">class </em><code class="descname">CurrentSiteMiddleware</code><a class="reference internal" href="../_modules/django/contrib/sites/middleware.html#CurrentSiteMiddleware"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.sites.middleware.CurrentSiteMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Adds the <code class="docutils literal"><span class="pre">site</span></code> attribute representing the current site to every incoming
<code class="docutils literal"><span class="pre">HttpRequest</span></code> object. See the <a class="reference internal" href="contrib/sites.html#site-middleware"><span class="std std-ref">sites documentation</span></a>.</p>
</div>
<div class="section" id="s-module-django.contrib.auth.middleware">
<span id="s-authentication-middleware"></span><span id="module-django.contrib.auth.middleware"></span><span id="authentication-middleware"></span><h3>Authentication middleware<a class="headerlink" href="#module-django.contrib.auth.middleware" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.contrib.auth.middleware.AuthenticationMiddleware">
<em class="property">class </em><code class="descname">AuthenticationMiddleware</code><a class="headerlink" href="#django.contrib.auth.middleware.AuthenticationMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Adds the <code class="docutils literal"><span class="pre">user</span></code> attribute, representing the currently-logged-in user, to
every incoming <code class="docutils literal"><span class="pre">HttpRequest</span></code> object. See <a class="reference internal" href="../topics/auth/default.html#auth-web-requests"><span class="std std-ref">Authentication in Web requests</span></a>.</p>
<dl class="class">
<dt id="django.contrib.auth.middleware.RemoteUserMiddleware">
<em class="property">class </em><code class="descname">RemoteUserMiddleware</code><a class="headerlink" href="#django.contrib.auth.middleware.RemoteUserMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Middleware for utilizing Web server provided authentication. See
<a class="reference internal" href="../howto/auth-remote-user.html"><span class="doc">Authentication using REMOTE_USER</span></a> for usage details.</p>
<dl class="class">
<dt id="django.contrib.auth.middleware.SessionAuthenticationMiddleware">
<em class="property">class </em><code class="descname">SessionAuthenticationMiddleware</code><a class="headerlink" href="#django.contrib.auth.middleware.SessionAuthenticationMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Allows a user&#8217;s sessions to be invalidated when their password changes. See
<a class="reference internal" href="../topics/auth/default.html#session-invalidation-on-password-change"><span class="std std-ref">Session invalidation on password change</span></a> for details. This middleware must
appear after <a class="reference internal" href="#django.contrib.auth.middleware.AuthenticationMiddleware" title="django.contrib.auth.middleware.AuthenticationMiddleware"><code class="xref py py-class docutils literal"><span class="pre">django.contrib.auth.middleware.AuthenticationMiddleware</span></code></a>
in <a class="reference internal" href="settings.html#std:setting-MIDDLEWARE_CLASSES"><code class="xref std std-setting docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></code></a>.</p>
</div>
<div class="section" id="s-module-django.middleware.csrf">
<span id="s-csrf-protection-middleware"></span><span id="module-django.middleware.csrf"></span><span id="csrf-protection-middleware"></span><h3>CSRF protection middleware<a class="headerlink" href="#module-django.middleware.csrf" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.middleware.csrf.CsrfViewMiddleware">
<em class="property">class </em><code class="descname">CsrfViewMiddleware</code><a class="headerlink" href="#django.middleware.csrf.CsrfViewMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Adds protection against Cross Site Request Forgeries by adding hidden form
fields to POST forms and checking requests for the correct value. See the
<a class="reference internal" href="csrf.html"><span class="doc">Cross Site Request Forgery protection documentation</span></a>.</p>
</div>
<div class="section" id="s-module-django.middleware.clickjacking">
<span id="s-x-frame-options-middleware"></span><span id="module-django.middleware.clickjacking"></span><span id="x-frame-options-middleware"></span><h3>X-Frame-Options middleware<a class="headerlink" href="#module-django.middleware.clickjacking" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.middleware.clickjacking.XFrameOptionsMiddleware">
<em class="property">class </em><code class="descname">XFrameOptionsMiddleware</code><a class="reference internal" href="../_modules/django/middleware/clickjacking.html#XFrameOptionsMiddleware"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.middleware.clickjacking.XFrameOptionsMiddleware" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Simple <a class="reference internal" href="clickjacking.html"><span class="doc">clickjacking protection via the X-Frame-Options header</span></a>.</p>
</div>
</div>
<div class="section" id="s-middleware-ordering">
<span id="s-id4"></span><span id="middleware-ordering"></span><span id="id4"></span><h2>Middleware ordering<a class="headerlink" href="#middleware-ordering" title="Permalink to this headline">¶</a></h2>
<p>Here are some hints about the ordering of various Django middleware classes:</p>
<ol class="arabic">
<li><p class="first"><a class="reference internal" href="#django.middleware.cache.UpdateCacheMiddleware" title="django.middleware.cache.UpdateCacheMiddleware"><code class="xref py py-class docutils literal"><span class="pre">UpdateCacheMiddleware</span></code></a></p>
<p>Before those that modify the <code class="docutils literal"><span class="pre">Vary</span></code> header (<code class="docutils literal"><span class="pre">SessionMiddleware</span></code>,
<code class="docutils literal"><span class="pre">GZipMiddleware</span></code>, <code class="docutils literal"><span class="pre">LocaleMiddleware</span></code>).</p>
</li>
<li><p class="first"><a class="reference internal" href="#django.middleware.gzip.GZipMiddleware" title="django.middleware.gzip.GZipMiddleware"><code class="xref py py-class docutils literal"><span class="pre">GZipMiddleware</span></code></a></p>
<p>Before any middleware that may change or use the response body.</p>
<p>After <code class="docutils literal"><span class="pre">UpdateCacheMiddleware</span></code>: Modifies <code class="docutils literal"><span class="pre">Vary</span></code> header.</p>
</li>
<li><p class="first"><a class="reference internal" href="#django.middleware.http.ConditionalGetMiddleware" title="django.middleware.http.ConditionalGetMiddleware"><code class="xref py py-class docutils literal"><span class="pre">ConditionalGetMiddleware</span></code></a></p>
<p>Before <code class="docutils literal"><span class="pre">CommonMiddleware</span></code>: uses its <code class="docutils literal"><span class="pre">Etag</span></code> header when
<a class="reference internal" href="settings.html#std:setting-USE_ETAGS"><code class="xref std std-setting docutils literal"><span class="pre">USE_ETAGS</span></code></a> = <code class="docutils literal"><span class="pre">True</span></code>.</p>
</li>
<li><p class="first"><a class="reference internal" href="#django.contrib.sessions.middleware.SessionMiddleware" title="django.contrib.sessions.middleware.SessionMiddleware"><code class="xref py py-class docutils literal"><span class="pre">SessionMiddleware</span></code></a></p>
<p>After <code class="docutils literal"><span class="pre">UpdateCacheMiddleware</span></code>: Modifies <code class="docutils literal"><span class="pre">Vary</span></code> header.</p>
</li>
<li><p class="first"><a class="reference internal" href="#django.middleware.locale.LocaleMiddleware" title="django.middleware.locale.LocaleMiddleware"><code class="xref py py-class docutils literal"><span class="pre">LocaleMiddleware</span></code></a></p>
<p>One of the topmost, after <code class="docutils literal"><span class="pre">SessionMiddleware</span></code> (uses session data) and
<code class="docutils literal"><span class="pre">UpdateCacheMiddleware</span></code> (modifies <code class="docutils literal"><span class="pre">Vary</span></code> header).</p>
</li>
<li><p class="first"><a class="reference internal" href="#django.middleware.common.CommonMiddleware" title="django.middleware.common.CommonMiddleware"><code class="xref py py-class docutils literal"><span class="pre">CommonMiddleware</span></code></a></p>
<p>Before any middleware that may change the response (it calculates <code class="docutils literal"><span class="pre">ETags</span></code>).</p>
<p>After <code class="docutils literal"><span class="pre">GZipMiddleware</span></code> so it won&#8217;t calculate an <code class="docutils literal"><span class="pre">ETag</span></code> header on gzipped
contents.</p>
<p>Close to the top: it redirects when <a class="reference internal" href="settings.html#std:setting-APPEND_SLASH"><code class="xref std std-setting docutils literal"><span class="pre">APPEND_SLASH</span></code></a> or
<a class="reference internal" href="settings.html#std:setting-PREPEND_WWW"><code class="xref std std-setting docutils literal"><span class="pre">PREPEND_WWW</span></code></a> are set to <code class="docutils literal"><span class="pre">True</span></code>.</p>
</li>
<li><p class="first"><a class="reference internal" href="#django.middleware.csrf.CsrfViewMiddleware" title="django.middleware.csrf.CsrfViewMiddleware"><code class="xref py py-class docutils literal"><span class="pre">CsrfViewMiddleware</span></code></a></p>
<p>Before any view middleware that assumes that CSRF attacks have been dealt
with.</p>
</li>
<li><p class="first"><a class="reference internal" href="#django.contrib.auth.middleware.AuthenticationMiddleware" title="django.contrib.auth.middleware.AuthenticationMiddleware"><code class="xref py py-class docutils literal"><span class="pre">AuthenticationMiddleware</span></code></a></p>
<p>After <code class="docutils literal"><span class="pre">SessionMiddleware</span></code>: uses session storage.</p>
</li>
<li><p class="first"><a class="reference internal" href="#django.contrib.messages.middleware.MessageMiddleware" title="django.contrib.messages.middleware.MessageMiddleware"><code class="xref py py-class docutils literal"><span class="pre">MessageMiddleware</span></code></a></p>
<p>After <code class="docutils literal"><span class="pre">SessionMiddleware</span></code>: can use session-based storage.</p>
</li>
<li><p class="first"><a class="reference internal" href="#django.middleware.cache.FetchFromCacheMiddleware" title="django.middleware.cache.FetchFromCacheMiddleware"><code class="xref py py-class docutils literal"><span class="pre">FetchFromCacheMiddleware</span></code></a></p>
<p>After any middleware that modifies the <code class="docutils literal"><span class="pre">Vary</span></code> header: that header is used
to pick a value for the cache hash-key.</p>
</li>
<li><p class="first"><a class="reference internal" href="contrib/flatpages.html#django.contrib.flatpages.middleware.FlatpageFallbackMiddleware" title="django.contrib.flatpages.middleware.FlatpageFallbackMiddleware"><code class="xref py py-class docutils literal"><span class="pre">FlatpageFallbackMiddleware</span></code></a></p>
<p>Should be near the bottom as it&#8217;s a last-resort type of middleware.</p>
</li>
<li><p class="first"><a class="reference internal" href="contrib/redirects.html#django.contrib.redirects.middleware.RedirectFallbackMiddleware" title="django.contrib.redirects.middleware.RedirectFallbackMiddleware"><code class="xref py py-class docutils literal"><span class="pre">RedirectFallbackMiddleware</span></code></a></p>
<p>Should be near the bottom as it&#8217;s a last-resort type of middleware.</p>
</li>
</ol>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Middleware</a><ul>
<li><a class="reference internal" href="#available-middleware">Available middleware</a><ul>
<li><a class="reference internal" href="#module-django.middleware.cache">Cache middleware</a></li>
<li><a class="reference internal" href="#module-django.middleware.common">&#8220;Common&#8221; middleware</a></li>
<li><a class="reference internal" href="#module-django.middleware.gzip">GZip middleware</a></li>
<li><a class="reference internal" href="#module-django.middleware.http">Conditional GET middleware</a></li>
<li><a class="reference internal" href="#module-django.middleware.locale">Locale middleware</a></li>
<li><a class="reference internal" href="#module-django.contrib.messages.middleware">Message middleware</a></li>
<li><a class="reference internal" href="#module-django.middleware.security">Security middleware</a><ul>
<li><a class="reference internal" href="#http-strict-transport-security">HTTP Strict Transport Security</a></li>
<li><a class="reference internal" href="#x-content-type-options-nosniff"><code class="docutils literal"><span class="pre">X-Content-Type-Options:</span> <span class="pre">nosniff</span></code></a></li>
<li><a class="reference internal" href="#x-xss-protection-1-mode-block"><code class="docutils literal"><span class="pre">X-XSS-Protection:</span> <span class="pre">1;</span> <span class="pre">mode=block</span></code></a></li>
<li><a class="reference internal" href="#ssl-redirect">SSL Redirect</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-django.contrib.sessions.middleware">Session middleware</a></li>
<li><a class="reference internal" href="#module-django.contrib.sites.middleware">Site middleware</a></li>
<li><a class="reference internal" href="#module-django.contrib.auth.middleware">Authentication middleware</a></li>
<li><a class="reference internal" href="#module-django.middleware.csrf">CSRF protection middleware</a></li>
<li><a class="reference internal" href="#module-django.middleware.clickjacking">X-Frame-Options middleware</a></li>
</ul>
</li>
<li><a class="reference internal" href="#middleware-ordering">Middleware ordering</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="forms/validation.html">Form and field validation</a></li>
    
    
      <li>Next: <a href="migration-operations.html">Migration Operations</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../index.html">Django 1.8.19 documentation</a>
        
          <ul><li><a href="index.html">API Reference</a>
        
        <ul><li>Middleware</li></ul>
        </li></ul>
      </li>
  </ul>

  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/ref/middleware.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 10, 2018</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="forms/validation.html" title="Form and field validation">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="migration-operations.html" title="Migration Operations">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>