{"title":"Middleware","version":"6.0","locale":"it","docname":"ref/middleware","url":"/it/6.0/ref/middleware/","canonical":"https://djangodocs.dev/it/6.0/ref/middleware/","summary":"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 middleware…","html":"<span id=\"middleware\"></span><h1>Middleware<a class=\"heading-anchor\" href=\"#module-django.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document explains all middleware components that come with Django. For\ninformation on how to use them and how to write your own middleware, see\nthe <a class=\"reference internal\" href=\"/it/6.0/topics/http/middleware/\"><span class=\"doc\">middleware usage guide</span></a>.</p>\n<section id=\"available-middleware\">\n<h2>Available middleware<a class=\"heading-anchor\" href=\"#available-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"module-django.middleware.cache\">\n<span id=\"cache-middleware\"></span><h3>Cache middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.cache\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.cache.UpdateCacheMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">UpdateCacheMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.cache.UpdateCacheMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.cache.FetchFromCacheMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">FetchFromCacheMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.cache.FetchFromCacheMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Enable the site-wide cache. If these are enabled, each Django-powered page will\nbe cached for as long as the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CACHE_MIDDLEWARE_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CACHE_MIDDLEWARE_SECONDS</span></code></a> setting\ndefines. See the <a class=\"reference internal\" href=\"/it/6.0/topics/cache/\"><span class=\"doc\">cache documentation</span></a>.</p>\n</section>\n<section id=\"module-django.middleware.common\">\n<span id=\"common-middleware\"></span><h3>«Common» middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.common\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.common.CommonMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CommonMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.common.CommonMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.common.CommonMiddleware.response_redirect_class\">\n<span class=\"sig-name descname\"><span class=\"pre\">response_redirect_class</span></span><a class=\"heading-anchor\" href=\"#django.middleware.common.CommonMiddleware.response_redirect_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Defaults to <a class=\"reference internal\" href=\"/it/6.0/ref/request-response/#django.http.HttpResponsePermanentRedirect\" title=\"django.http.HttpResponsePermanentRedirect\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponsePermanentRedirect</span></code></a>.\nSubclass <code class=\"docutils literal notranslate\"><span class=\"pre\">CommonMiddleware</span></code> and override the attribute to customize\nthe redirects issued by the middleware.</p>\n</dd></dl>\n\n</dd></dl>\n\n<p>Adds a few conveniences for perfectionists:</p>\n<ul>\n<li><p>Forbids access to user agents in the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-DISALLOWED_USER_AGENTS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DISALLOWED_USER_AGENTS</span></code></a>\nsetting, which should be a list of compiled regular expression objects.</p></li>\n<li><p>Performs URL rewriting based on the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-APPEND_SLASH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APPEND_SLASH</span></code></a> and\n<a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-PREPEND_WWW\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PREPEND_WWW</span></code></a> settings.</p>\n<p>If <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-APPEND_SLASH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APPEND_SLASH</span></code></a> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> and the initial URL doesn’t end\nwith a slash, and it is not found in the URLconf, then a new URL is\nformed by appending a slash at the end. If this new URL is found in the\nURLconf, then Django redirects the request to this new URL. Otherwise,\nthe initial URL is processed as usual.</p>\n<p>For example, <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar</span></code> will be redirected to <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar/</span></code> if\nyou don’t have a valid URL pattern for <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar</span></code> but <em>do</em> have a\nvalid pattern for <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar/</span></code>.</p>\n<p>If <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-PREPEND_WWW\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PREPEND_WWW</span></code></a> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, URLs that lack a leading «www.»\nwill be redirected to the same URL with a leading «www.»</p>\n<p>Both of these options are meant to normalize URLs. The philosophy is that\neach URL should exist in one, and only one, place. Technically a URL\n<code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar</span></code> is distinct from <code class=\"docutils literal notranslate\"><span class=\"pre\">foo.com/bar/</span></code> – a search-engine\nindexer would treat them as separate URLs – so it’s best practice to\nnormalize URLs.</p>\n<p>If necessary, individual views may be excluded from the <code class=\"docutils literal notranslate\"><span class=\"pre\">APPEND_SLASH</span></code>\nbehavior using the <a class=\"reference internal\" href=\"/it/6.0/topics/http/decorators/#django.views.decorators.common.no_append_slash\" title=\"django.views.decorators.common.no_append_slash\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">no_append_slash()</span></code></a>\ndecorator:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.views.decorators.common</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">no_append_slash</span>\n\n\n<span class=\"nd\">@no_append_slash</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">sensitive_fbv</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;View to be excluded from APPEND_SLASH.&quot;&quot;&quot;</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">()</span>\n</code></pre></div>\n</li>\n<li><p>Sets the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code> header for non-streaming responses.</p></li>\n</ul>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.common.BrokenLinkEmailsMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">BrokenLinkEmailsMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.common.BrokenLinkEmailsMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<ul class=\"simple\">\n<li><p>Sends broken link notification emails to <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-MANAGERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MANAGERS</span></code></a> (see\n<a class=\"reference internal\" href=\"/it/6.0/howto/error-reporting/\"><span class=\"doc\">Come gestire il report degli errori</span></a>).</p></li>\n</ul>\n</section>\n<section id=\"module-django.middleware.gzip\">\n<span id=\"gzip-middleware\"></span><h3>GZip middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.gzip\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.gzip.GZipMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">GZipMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.gzip.GZipMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.gzip.GZipMiddleware.max_random_bytes\">\n<span class=\"sig-name descname\"><span class=\"pre\">max_random_bytes</span></span><a class=\"heading-anchor\" href=\"#django.middleware.gzip.GZipMiddleware.max_random_bytes\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Defaults to 100. Subclass <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code> and override the attribute\nto change the maximum number of random bytes that is included with\ncompressed responses.</p>\n</dd></dl>\n\n</dd></dl>\n\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>Security researchers revealed that when compression techniques (including\n<code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code>) are used on a website, the site may become exposed to a\nnumber of possible attacks.</p>\n<p>To mitigate attacks, Django implements a technique called <em>Heal The Breach\n(HTB)</em>. It adds up to 100 bytes (see\n<a class=\"reference internal\" href=\"#django.middleware.gzip.GZipMiddleware.max_random_bytes\" title=\"django.middleware.gzip.GZipMiddleware.max_random_bytes\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">max_random_bytes</span></code></a>) of random bytes to each response\nto make the attacks less effective.</p>\n<p>For more details, see the <a class=\"reference external\" href=\"https://www.breachattack.com/resources/BREACH%20-%20SSL,%20gone%20in%2030%20seconds.pdf\">BREACH paper (PDF)</a>, <a class=\"reference external\" href=\"https://www.breachattack.com/\">breachattack.com</a>, and\nthe <a class=\"reference external\" href=\"https://ieeexplore.ieee.org/document/9754554\">Heal The Breach (HTB) paper</a>.</p>\n</aside>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">django.middleware.gzip.GZipMiddleware</span></code> compresses content for browsers\nthat understand GZip compression (all modern browsers).</p>\n<p>This middleware should be placed before any other middleware that need to\nread or write the response body so that compression happens afterward.</p>\n<p>It will NOT compress content if any of the following are true:</p>\n<ul class=\"simple\">\n<li><p>The content body is less than 200 bytes long.</p></li>\n<li><p>The response has already set the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Encoding</span></code> header.</p></li>\n<li><p>The request (the browser) hasn’t sent an <code class=\"docutils literal notranslate\"><span class=\"pre\">Accept-Encoding</span></code> header\ncontaining <code class=\"docutils literal notranslate\"><span class=\"pre\">gzip</span></code>.</p></li>\n</ul>\n<p>If the response has an <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code> header, the ETag is made weak to comply with\n<span class=\"target\" id=\"index-0\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc9110.html#section-8.8.1\"><strong>RFC 9110 Section 8.8.1</strong></a>.</p>\n<p>You can apply GZip compression to individual views using the\n<a class=\"reference internal\" href=\"/it/6.0/topics/http/decorators/#django.views.decorators.gzip.gzip_page\" title=\"django.views.decorators.gzip.gzip_page\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">gzip_page()</span></code></a> decorator.</p>\n</section>\n<section id=\"module-django.middleware.http\">\n<span id=\"conditional-get-middleware\"></span><h3>Conditional GET middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.http\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.http.ConditionalGetMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">ConditionalGetMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.http.ConditionalGetMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Handles conditional GET operations. If the response doesn’t have an <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code>\nheader, the middleware adds one if needed. If the response has an <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Last-Modified</span></code> header, and the request has <code class=\"docutils literal notranslate\"><span class=\"pre\">If-None-Match</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">If-Modified-Since</span></code>, the response is replaced by an\n<a class=\"reference internal\" href=\"/it/6.0/ref/request-response/#django.http.HttpResponseNotModified\" title=\"django.http.HttpResponseNotModified\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponseNotModified</span></code></a>.</p>\n<p>You can handle conditional GET operations with individual views using the\n<a class=\"reference internal\" href=\"/it/6.0/topics/http/decorators/#django.views.decorators.http.conditional_page\" title=\"django.views.decorators.http.conditional_page\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">conditional_page()</span></code></a> decorator.</p>\n</section>\n<section id=\"module-django.middleware.locale\">\n<span id=\"locale-middleware\"></span><h3>Locale middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.locale\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.locale.LocaleMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">LocaleMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.locale.LocaleMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.locale.LocaleMiddleware.response_redirect_class\">\n<span class=\"sig-name descname\"><span class=\"pre\">response_redirect_class</span></span><a class=\"heading-anchor\" href=\"#django.middleware.locale.LocaleMiddleware.response_redirect_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Defaults to <a class=\"reference internal\" href=\"/it/6.0/ref/request-response/#django.http.HttpResponseRedirect\" title=\"django.http.HttpResponseRedirect\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponseRedirect</span></code></a>. Subclass\n<code class=\"docutils literal notranslate\"><span class=\"pre\">LocaleMiddleware</span></code> and override the attribute to customize the\nredirects issued by the middleware.</p>\n</dd></dl>\n\n</dd></dl>\n\n<p>Enables language selection based on data from the request. It customizes\ncontent for each user. See the <a class=\"reference internal\" href=\"/it/6.0/topics/i18n/translation/\"><span class=\"doc\">internationalization documentation</span></a>.</p>\n</section>\n<section id=\"module-django.contrib.messages.middleware\">\n<span id=\"message-middleware\"></span><h3>Message middleware<a class=\"heading-anchor\" href=\"#module-django.contrib.messages.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.messages.middleware.MessageMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">MessageMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.messages.middleware.MessageMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Enables cookie- and session-based message support. See the\n<a class=\"reference internal\" href=\"/it/6.0/ref/contrib/messages/\"><span class=\"doc\">messages documentation</span></a>.</p>\n</section>\n<section id=\"module-django.middleware.security\">\n<span id=\"id1\"></span><span id=\"security-middleware\"></span><h3>Security middleware<a class=\"heading-anchor\" href=\"#module-django.middleware.security\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Avvertimento</p>\n<p>If your deployment situation allows, it’s usually a good idea to have your\nfront-end web server perform the functionality provided by the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code>. That way, if there are requests that aren’t served\nby Django (such as static media or user-uploaded files), they will have\nthe same protections as requests to your Django application.</p>\n</aside>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.security.SecurityMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">SecurityMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.security.SecurityMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">django.middleware.security.SecurityMiddleware</span></code> provides several security\nenhancements to the request/response cycle. Each one can be independently\nenabled or disabled with a setting.</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_CONTENT_TYPE_NOSNIFF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CONTENT_TYPE_NOSNIFF</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_CROSS_ORIGIN_OPENER_POLICY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CROSS_ORIGIN_OPENER_POLICY</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_HSTS_INCLUDE_SUBDOMAINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_INCLUDE_SUBDOMAINS</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_HSTS_PRELOAD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_PRELOAD</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_HSTS_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_SECONDS</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_REDIRECT_EXEMPT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REDIRECT_EXEMPT</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_REFERRER_POLICY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REFERRER_POLICY</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_SSL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_HOST</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_SSL_REDIRECT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_REDIRECT</span></code></a></p></li>\n</ul>\n<section id=\"http-strict-transport-security\">\n<span id=\"id2\"></span><h4>HTTP Strict Transport Security<a class=\"heading-anchor\" href=\"#http-strict-transport-security\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>For sites that should only be accessed over HTTPS, you can instruct modern\nbrowsers to refuse to connect to your domain name via an insecure connection\n(for a given period of time) by setting the <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security\">«Strict-Transport-Security»\nheader</a>. This reduces your exposure to some SSL-stripping man-in-the-middle\n(MITM) attacks.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will set this header for you on all HTTPS responses if\nyou set the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_HSTS_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_SECONDS</span></code></a> setting to a non-zero integer value.</p>\n<p>When enabling HSTS, it’s a good idea to first use a small value for testing,\nfor example, <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_HSTS_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_SECONDS</span> <span class=\"pre\">=</span> <span class=\"pre\">3600</span></code></a> for one\nhour. Each time a web browser sees the HSTS header from your site, it will\nrefuse to communicate non-securely (using HTTP) with your domain for the given\nperiod of time. Once you confirm that all assets are served securely on your\nsite (i.e. HSTS didn’t break anything), it’s a good idea to increase this value\nso that infrequent visitors will be protected (31536000 seconds, i.e. 1 year,\nis common).</p>\n<p>Additionally, if you set the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_HSTS_INCLUDE_SUBDOMAINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_INCLUDE_SUBDOMAINS</span></code></a> setting\nto <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will add the <code class=\"docutils literal notranslate\"><span class=\"pre\">includeSubDomains</span></code>\ndirective to the <code class=\"docutils literal notranslate\"><span class=\"pre\">Strict-Transport-Security</span></code> header. This is recommended\n(assuming all subdomains are served exclusively using HTTPS), otherwise your\nsite may still be vulnerable via an insecure connection to a subdomain.</p>\n<p>If you wish to submit your site to the <a class=\"reference external\" href=\"https://hstspreload.org/\">browser preload list</a>, set the\n<a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_HSTS_PRELOAD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_PRELOAD</span></code></a> setting to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>. That appends the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">preload</span></code> directive to the <code class=\"docutils literal notranslate\"><span class=\"pre\">Strict-Transport-Security</span></code> header.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Avvertimento</p>\n<p>The HSTS policy applies to your entire domain, not just the URL of the\nresponse that you set the header on. Therefore, you should only use it if\nyour entire domain is served via HTTPS only.</p>\n<p>Browsers properly respecting the HSTS header will refuse to allow users to\nbypass warnings and connect to a site with an expired, self-signed, or\notherwise invalid SSL certificate. If you use HSTS, make sure your\ncertificates are in good shape and stay that way!</p>\n</aside>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>If you are deployed behind a load-balancer or reverse-proxy server, and the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Strict-Transport-Security</span></code> header is not being added to your responses,\nit may be because Django doesn’t realize that it’s on a secure connection;\nyou may need to set the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_PROXY_SSL_HEADER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_PROXY_SSL_HEADER</span></code></a> setting.</p>\n</aside>\n</section>\n<section id=\"referrer-policy\">\n<span id=\"id4\"></span><h4>Referrer Policy<a class=\"heading-anchor\" href=\"#referrer-policy\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Browsers use <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer\">the Referer header</a> as a way to send information to a site\nabout how users got there. When a user clicks a link, the browser will send the\nfull URL of the linking page as the referrer. While this can be useful for some\npurposes – like figuring out who’s linking to your site – it also can cause\nprivacy concerns by informing one site that a user was visiting another site.</p>\n<p>Some browsers have the ability to accept hints about whether they should send\nthe HTTP <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header when a user clicks a link; this hint is provided\nvia <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy\">the Referrer-Policy header</a>. This header can suggest any of three\nbehaviors to browsers:</p>\n<ul class=\"simple\">\n<li><p>Full URL: send the entire URL in the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header. For example, if the\nuser is visiting <code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/page.html</span></code>, the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header\nwould contain <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;https://example.com/page.html&quot;</span></code>.</p></li>\n<li><p>Origin only: send only the «origin» in the referrer. The origin consists of\nthe scheme, host and (optionally) port number. For example, if the user is\nvisiting <code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/page.html</span></code>, the origin would be\n<code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/</span></code>.</p></li>\n<li><p>No referrer: do not send a <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header at all.</p></li>\n</ul>\n<p>There are two types of conditions this header can tell a browser to watch out\nfor:</p>\n<ul class=\"simple\">\n<li><p>Same-origin versus cross-origin: a link from <code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/1.html</span></code>\nto <code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/2.html</span></code> is same-origin. A link from\n<code class=\"docutils literal notranslate\"><span class=\"pre\">https://example.com/page.html</span></code> to <code class=\"docutils literal notranslate\"><span class=\"pre\">https://not.example.com/page.html</span></code> is\ncross-origin.</p></li>\n<li><p>Protocol downgrade: a downgrade occurs if the page containing the link is\nserved via HTTPS, but the page being linked to is not served via HTTPS.</p></li>\n</ul>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Avvertimento</p>\n<p>When your site is served via HTTPS, <a class=\"reference internal\" href=\"/it/6.0/ref/csrf/#how-csrf-works\"><span class=\"std std-ref\">Django’s CSRF protection system</span></a> requires the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header to be present, so\ncompletely disabling the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header will interfere with CSRF\nprotection. To gain most of the benefits of disabling <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> headers\nwhile also keeping CSRF protection, consider enabling only same-origin\nreferrers.</p>\n</aside>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> can set the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referrer-Policy</span></code> header for you, based on\nthe <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_REFERRER_POLICY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REFERRER_POLICY</span></code></a> setting (note spelling: browsers send a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header when a user clicks a link, but the header instructing a\nbrowser whether to do so is spelled <code class=\"docutils literal notranslate\"><span class=\"pre\">Referrer-Policy</span></code>). The valid values for\nthis setting are:</p>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">no-referrer</span></code></dt><dd><p>Instructs the browser to send no referrer for links clicked on this site.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">no-referrer-when-downgrade</span></code></dt><dd><p>Instructs the browser to send a full URL as the referrer, but only when no\nprotocol downgrade occurs.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">origin</span></code></dt><dd><p>Instructs the browser to send only the origin, not the full URL, as the\nreferrer.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">origin-when-cross-origin</span></code></dt><dd><p>Instructs the browser to send the full URL as the referrer for same-origin\nlinks, and only the origin for cross-origin links.</p>\n</dd>\n</dl>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">same-origin</span></code> Instructs the browser to send a full URL, but only for\nsame-origin links. No referrer will be sent for cross-origin links.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">strict-origin</span></code> Instructs the browser to send only the origin, not the full\nURL, and to send no referrer when a protocol downgrade occurs.</p>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">strict-origin-when-cross-origin</span></code></dt><dd><p>Instructs the browser to send the full URL when the link is same-origin and\nno protocol downgrade occurs; send only the origin when the link is\ncross-origin and no protocol downgrade occurs; and no referrer when a\nprotocol downgrade occurs.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">unsafe-url</span></code></dt><dd><p>Instructs the browser to always send the full URL as the referrer.</p>\n</dd>\n</dl>\n<aside class=\"admonition-unknown-policy-values admonition\">\n<p class=\"admonition-title\">Unknown Policy Values</p>\n<p>Where a policy value is <a class=\"reference external\" href=\"https://w3c.github.io/webappsec-referrer-policy/#unknown-policy-values\">unknown</a> by a user agent, it is possible to\nspecify multiple policy values to provide a fallback. The last specified\nvalue that is understood takes precedence. To support this, an iterable or\ncomma-separated string can be used with <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_REFERRER_POLICY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REFERRER_POLICY</span></code></a>.</p>\n</aside>\n</section>\n<section id=\"cross-origin-opener-policy\">\n<span id=\"id8\"></span><h4>Cross-Origin Opener Policy<a class=\"heading-anchor\" href=\"#cross-origin-opener-policy\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Some browsers have the ability to isolate top-level windows from other\ndocuments by putting them in a separate browsing context group based on the\nvalue of the <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy\">Cross-Origin Opener Policy</a> (COOP) header. If a document that\nis isolated in this way opens a cross-origin popup window, the popup’s\n<code class=\"docutils literal notranslate\"><span class=\"pre\">window.opener</span></code> property will be <code class=\"docutils literal notranslate\"><span class=\"pre\">null</span></code>. Isolating windows using COOP is a\ndefense-in-depth protection against cross-origin attacks, especially those like\nSpectre which allowed exfiltration of data loaded into a shared browsing\ncontext.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> can set the <code class=\"docutils literal notranslate\"><span class=\"pre\">Cross-Origin-Opener-Policy</span></code> header for\nyou, based on the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_CROSS_ORIGIN_OPENER_POLICY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CROSS_ORIGIN_OPENER_POLICY</span></code></a> setting. The\nvalid values for this setting are:</p>\n<dl class=\"simple\">\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">same-origin</span></code></dt><dd><p>Isolates the browsing context exclusively to same-origin documents.\nCross-origin documents are not loaded in the same browsing context. This\nis the default and most secure option.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">same-origin-allow-popups</span></code></dt><dd><p>Isolates the browsing context to same-origin documents or those which\neither don’t set COOP or which opt out of isolation by setting a COOP of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">unsafe-none</span></code>.</p>\n</dd>\n<dt><code class=\"docutils literal notranslate\"><span class=\"pre\">unsafe-none</span></code></dt><dd><p>Allows the document to be added to its opener’s browsing context group\nunless the opener itself has a COOP of <code class=\"docutils literal notranslate\"><span class=\"pre\">same-origin</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">same-origin-allow-popups</span></code>.</p>\n</dd>\n</dl>\n</section>\n<section id=\"x-content-type-options-nosniff\">\n<span id=\"x-content-type-options\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">X-Content-Type-Options:</span> <span class=\"pre\">nosniff</span></code><a class=\"heading-anchor\" href=\"#x-content-type-options-nosniff\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Some browsers will try to guess the content types of the assets that they\nfetch, overriding the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code> header. While this can help display\nsites with improperly configured servers, it can also pose a security\nrisk.</p>\n<p>If your site serves user-uploaded files, a malicious user could upload a\nspecially-crafted file that would be interpreted as HTML or JavaScript by\nthe browser when you expected it to be something harmless.</p>\n<p>To prevent the browser from guessing the content type and force it to\nalways use the type provided in the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code> header, you can pass\nthe <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options\">X-Content-Type-Options: nosniff</a> header. <code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will\ndo this for all responses if the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_CONTENT_TYPE_NOSNIFF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CONTENT_TYPE_NOSNIFF</span></code></a> setting\nis <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n<p>Note that in most deployment situations where Django isn’t involved in serving\nuser-uploaded files, this setting won’t help you. For example, if your\n<a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a> is served directly by your front-end web server (nginx,\nApache, etc.) then you’d want to set this header there. On the other hand, if\nyou are using Django to do something like require authorization in order to\ndownload files and you cannot set the header using your web server, this\nsetting will be useful.</p>\n</section>\n<section id=\"ssl-redirect\">\n<span id=\"id11\"></span><h4>SSL Redirect<a class=\"heading-anchor\" href=\"#ssl-redirect\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>If your site offers both HTTP and HTTPS connections, most users will end up\nwith an unsecured connection by default. For best security, you should redirect\nall HTTP connections to HTTPS.</p>\n<p>If you set the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_SSL_REDIRECT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_REDIRECT</span></code></a> setting to True,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code> will permanently (HTTP 301) redirect all HTTP\nconnections to HTTPS.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>For performance reasons, it’s preferable to do these redirects outside of\nDjango, in a front-end load balancer or reverse-proxy server such as\n<a class=\"reference external\" href=\"https://nginx.org/\">nginx</a>. <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_SSL_REDIRECT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_REDIRECT</span></code></a> is intended for the deployment\nsituations where this isn’t an option.</p>\n</aside>\n<p>If the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_SSL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_HOST</span></code></a> setting has a value, all redirects will be\nsent to that host instead of the originally-requested host.</p>\n<p>If there are a few pages on your site that should be available over HTTP, and\nnot redirected to HTTPS, you can list regular expressions to match those URLs\nin the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_REDIRECT_EXEMPT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_REDIRECT_EXEMPT</span></code></a> setting.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>If you are deployed behind a load-balancer or reverse-proxy server and\nDjango can’t seem to tell when a request actually is already secure, you\nmay need to set the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_PROXY_SSL_HEADER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_PROXY_SSL_HEADER</span></code></a> setting.</p>\n</aside>\n</section>\n</section>\n<section id=\"module-django.contrib.sessions.middleware\">\n<span id=\"session-middleware\"></span><h3>Session middleware<a class=\"heading-anchor\" href=\"#module-django.contrib.sessions.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sessions.middleware.SessionMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">SessionMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.sessions.middleware.SessionMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Enables session support. See the <a class=\"reference internal\" href=\"/it/6.0/topics/http/sessions/\"><span class=\"doc\">session documentation</span></a>.</p>\n</section>\n<section id=\"module-django.contrib.sites.middleware\">\n<span id=\"site-middleware\"></span><h3>Site middleware<a class=\"heading-anchor\" href=\"#module-django.contrib.sites.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sites.middleware.CurrentSiteMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CurrentSiteMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.sites.middleware.CurrentSiteMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Adds the <code class=\"docutils literal notranslate\"><span class=\"pre\">site</span></code> attribute representing the current site to every incoming\n<code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> object. See the <a class=\"reference internal\" href=\"/it/6.0/ref/contrib/sites/#site-middleware\"><span class=\"std std-ref\">sites documentation</span></a>.</p>\n</section>\n<section id=\"module-django.contrib.auth.middleware\">\n<span id=\"authentication-middleware\"></span><h3>Authentication middleware<a class=\"heading-anchor\" href=\"#module-django.contrib.auth.middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.AuthenticationMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">AuthenticationMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.AuthenticationMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Adds the <code class=\"docutils literal notranslate\"><span class=\"pre\">user</span></code> attribute, representing the currently-logged-in user, to\nevery incoming <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> object. See <a class=\"reference internal\" href=\"/it/6.0/topics/auth/default/#auth-web-requests\"><span class=\"std std-ref\">Authentication in web requests</span></a>.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.LoginRequiredMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">LoginRequiredMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.LoginRequiredMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Subclass the middleware and override the following attributes and methods\nto customize behavior for unauthenticated requests.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.LoginRequiredMiddleware.redirect_field_name\">\n<span class=\"sig-name descname\"><span class=\"pre\">redirect_field_name</span></span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.LoginRequiredMiddleware.redirect_field_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Defaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;next&quot;</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.LoginRequiredMiddleware.get_login_url\">\n<span class=\"sig-name descname\"><span class=\"pre\">get_login_url</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.LoginRequiredMiddleware.get_login_url\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the URL that unauthenticated requests will be redirected to.\nThis result is either the <code class=\"docutils literal notranslate\"><span class=\"pre\">login_url</span></code> set on the\n<a class=\"reference internal\" href=\"/it/6.0/topics/auth/default/#django.contrib.auth.decorators.login_required\" title=\"django.contrib.auth.decorators.login_required\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">login_required()</span></code></a> decorator (if\nnot <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code>), or <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-LOGIN_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">settings.LOGIN_URL</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.LoginRequiredMiddleware.get_redirect_field_name\">\n<span class=\"sig-name descname\"><span class=\"pre\">get_redirect_field_name</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.LoginRequiredMiddleware.get_redirect_field_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the name of the query parameter that contains the URL the user\nshould be redirected to after a successful login. This result is either\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">redirect_field_name</span></code> set on the\n<a class=\"reference internal\" href=\"/it/6.0/topics/auth/default/#django.contrib.auth.decorators.login_required\" title=\"django.contrib.auth.decorators.login_required\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">login_required()</span></code></a> decorator (if\nnot <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code>), or <a class=\"reference internal\" href=\"#django.contrib.auth.middleware.LoginRequiredMiddleware.redirect_field_name\" title=\"django.contrib.auth.middleware.LoginRequiredMiddleware.redirect_field_name\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">redirect_field_name</span></code></a>. If <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> is returned,\na query parameter won’t be added.</p>\n</dd></dl>\n\n</dd></dl>\n\n<p>Redirects all unauthenticated requests to a login page, except for views\nexcluded with <a class=\"reference internal\" href=\"/it/6.0/topics/auth/default/#django.contrib.auth.decorators.login_not_required\" title=\"django.contrib.auth.decorators.login_not_required\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">login_not_required()</span></code></a>. The\nlogin page defaults to <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-LOGIN_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">settings.LOGIN_URL</span></code></a>, but can be\ncustomized.</p>\n<p>Enable this middleware by adding it to the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a> setting\n<strong>after</strong> <a class=\"reference internal\" href=\"#django.contrib.auth.middleware.AuthenticationMiddleware\" title=\"django.contrib.auth.middleware.AuthenticationMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AuthenticationMiddleware</span></code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">MIDDLEWARE</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s2\">&quot;...&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.contrib.auth.middleware.AuthenticationMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.contrib.auth.middleware.LoginRequiredMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;...&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Make a view public, allowing unauthenticated requests, with\n<a class=\"reference internal\" href=\"/it/6.0/topics/auth/default/#django.contrib.auth.decorators.login_not_required\" title=\"django.contrib.auth.decorators.login_not_required\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">login_not_required()</span></code></a>. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.auth.decorators</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">login_not_required</span>\n\n\n<span class=\"nd\">@login_not_required</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">contact_us</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span> <span class=\"o\">...</span>\n</code></pre></div>\n<p>Customize the login URL or field name for authenticated views with the\n<a class=\"reference internal\" href=\"/it/6.0/topics/auth/default/#django.contrib.auth.decorators.login_required\" title=\"django.contrib.auth.decorators.login_required\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">login_required()</span></code></a> decorator to set\n<code class=\"docutils literal notranslate\"><span class=\"pre\">login_url</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">redirect_field_name</span></code> respectively. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.auth.decorators</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">login_required</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils.decorators</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">method_decorator</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.views.generic</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">View</span>\n\n\n<span class=\"nd\">@login_required</span><span class=\"p\">(</span><span class=\"n\">login_url</span><span class=\"o\">=</span><span class=\"s2\">&quot;/books/login/&quot;</span><span class=\"p\">,</span> <span class=\"n\">redirect_field_name</span><span class=\"o\">=</span><span class=\"s2\">&quot;redirect_to&quot;</span><span class=\"p\">)</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">book_dashboard</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span> <span class=\"o\">...</span>\n\n\n<span class=\"nd\">@method_decorator</span><span class=\"p\">(</span>\n    <span class=\"n\">login_required</span><span class=\"p\">(</span><span class=\"n\">login_url</span><span class=\"o\">=</span><span class=\"s2\">&quot;/books/login/&quot;</span><span class=\"p\">,</span> <span class=\"n\">redirect_field_name</span><span class=\"o\">=</span><span class=\"s2\">&quot;redirect_to&quot;</span><span class=\"p\">),</span>\n    <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;dispatch&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">BookMetrics</span><span class=\"p\">(</span><span class=\"n\">View</span><span class=\"p\">):</span>\n    <span class=\"k\">pass</span>\n</code></pre></div>\n<aside class=\"admonition-ensure-that-your-login-view-does-not-require-a-login admonition\">\n<p class=\"admonition-title\">Ensure that your login view does not require a login.</p>\n<p>To prevent infinite redirects, ensure you have\n<a class=\"reference internal\" href=\"/it/6.0/topics/auth/default/#disable-login-required-middleware-for-views\"><span class=\"std std-ref\">enabled unauthenticated requests</span></a> to your login view.</p>\n</aside>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.RemoteUserMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">RemoteUserMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.RemoteUserMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Middleware for utilizing web server provided authentication. See\n<a class=\"reference internal\" href=\"/it/6.0/howto/auth-remote-user/\"><span class=\"doc\">Come autenticarsi usando REMOTE_USER</span></a> for usage details, including security\nconsiderations.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.auth.middleware.PersistentRemoteUserMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">PersistentRemoteUserMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.auth.middleware.PersistentRemoteUserMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Middleware for utilizing web server provided authentication when enabled only\non the login page. See <a class=\"reference internal\" href=\"/it/6.0/howto/auth-remote-user/#persistent-remote-user-middleware-howto\"><span class=\"std std-ref\">Usare REMOTE_USER solo sulle pagine di login</span></a> for usage\ndetails, including security considerations.</p>\n</section>\n<section id=\"csrf-protection-middleware\">\n<h3>CSRF protection middleware<a class=\"heading-anchor\" href=\"#csrf-protection-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.csrf.CsrfViewMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">CsrfViewMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.csrf.CsrfViewMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Adds protection against Cross Site Request Forgeries by adding hidden form\nfields to POST forms and checking requests for the correct value. See the\n<a class=\"reference internal\" href=\"/it/6.0/ref/csrf/\"><span class=\"doc\">Cross Site Request Forgery protection documentation</span></a>.</p>\n<p>You can add Cross Site Request Forgery protection to individual views using the\n<a class=\"reference internal\" href=\"/it/6.0/ref/csrf/#django.views.decorators.csrf.csrf_protect\" title=\"django.views.decorators.csrf.csrf_protect\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">csrf_protect()</span></code></a> decorator.</p>\n</section>\n<section id=\"x-frame-options-middleware\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span></code> middleware<a class=\"heading-anchor\" href=\"#x-frame-options-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.clickjacking.XFrameOptionsMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">XFrameOptionsMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.clickjacking.XFrameOptionsMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Simple <a class=\"reference internal\" href=\"/it/6.0/ref/clickjacking/\"><span class=\"doc\">clickjacking protection via the X-Frame-Options header</span></a>.</p>\n</section>\n<section id=\"content-security-policy-middleware\">\n<h3>Content Security Policy middleware<a class=\"heading-anchor\" href=\"#content-security-policy-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.middleware.csp.ContentSecurityPolicyMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">ContentSecurityPolicyMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.middleware.csp.ContentSecurityPolicyMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"version-note version-added\" data-version=\"6.0\">\n<p class=\"version-note-title\">New in Django 6.0</p></aside>\n<p>Adds support for Content Security Policy (CSP), which helps mitigate risks such\nas Cross-Site Scripting (XSS) and data injection attacks by controlling the\nsources of content that can be loaded in the browser. See the\n<a class=\"reference internal\" href=\"/it/6.0/ref/csp/#csp-overview\"><span class=\"std std-ref\">Overview</span></a> documentation for details on configuring policies.</p>\n<p>This middleware sets the following headers on the response depending on the\navailable settings:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Security-Policy</span></code>, based on <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_CSP\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CSP</span></code></a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Security-Policy-Report-Only</span></code>, based on\n<a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-SECURE_CSP_REPORT_ONLY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_CSP_REPORT_ONLY</span></code></a>.</p></li>\n</ul>\n</section>\n</section>\n<section id=\"middleware-ordering\">\n<span id=\"id12\"></span><h2>Middleware ordering<a class=\"heading-anchor\" href=\"#middleware-ordering\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Here are some hints about the ordering of various Django middleware classes:</p>\n<ol class=\"arabic\">\n<li><p><a class=\"reference internal\" href=\"#django.middleware.security.SecurityMiddleware\" title=\"django.middleware.security.SecurityMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SecurityMiddleware</span></code></a></p>\n<p>It should go near the top of the list if you’re going to turn on the SSL\nredirect as that avoids running through a bunch of other unnecessary\nmiddleware.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.cache.UpdateCacheMiddleware\" title=\"django.middleware.cache.UpdateCacheMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code></a></p>\n<p>Before those that modify the <code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> header (<code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code>,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">LocaleMiddleware</span></code>).</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.gzip.GZipMiddleware\" title=\"django.middleware.gzip.GZipMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code></a></p>\n<p>Before any middleware that may change or use the response body.</p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code>: Modifies <code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> header.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.sessions.middleware.SessionMiddleware\" title=\"django.contrib.sessions.middleware.SessionMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code></a></p>\n<p>Before any middleware that may raise an exception to trigger an error\nview (such as <a class=\"reference internal\" href=\"/it/6.0/ref/exceptions/#django.core.exceptions.PermissionDenied\" title=\"django.core.exceptions.PermissionDenied\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">PermissionDenied</span></code></a>) if you’re\nusing <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_USE_SESSIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_USE_SESSIONS</span></code></a>.</p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code>: Modifies <code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> header.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.http.ConditionalGetMiddleware\" title=\"django.middleware.http.ConditionalGetMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ConditionalGetMiddleware</span></code></a></p>\n<p>Before any middleware that may change the response (it sets the <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code>\nheader).</p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">GZipMiddleware</span></code> so it won’t calculate an <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code> header on gzipped\ncontents.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.locale.LocaleMiddleware\" title=\"django.middleware.locale.LocaleMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">LocaleMiddleware</span></code></a></p>\n<p>One of the topmost, after <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code> (uses session data) and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">UpdateCacheMiddleware</span></code> (modifies <code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> header).</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.common.CommonMiddleware\" title=\"django.middleware.common.CommonMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CommonMiddleware</span></code></a></p>\n<p>Before any middleware that may change the response (it sets the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code> header). A middleware that appears before\n<code class=\"docutils literal notranslate\"><span class=\"pre\">CommonMiddleware</span></code> and changes the response must reset <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code>.</p>\n<p>Close to the top: it redirects when <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-APPEND_SLASH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">APPEND_SLASH</span></code></a> or\n<a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-PREPEND_WWW\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PREPEND_WWW</span></code></a> are set to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code> if you’re using <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_USE_SESSIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_USE_SESSIONS</span></code></a>.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.csrf.CsrfViewMiddleware\" title=\"django.middleware.csrf.CsrfViewMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware</span></code></a></p>\n<p>Before any view middleware that assumes that CSRF attacks have been dealt\nwith.</p>\n<p>Before <a class=\"reference internal\" href=\"#django.contrib.auth.middleware.RemoteUserMiddleware\" title=\"django.contrib.auth.middleware.RemoteUserMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RemoteUserMiddleware</span></code></a>, or any\nother authentication middleware that may perform a login, and hence rotate\nthe CSRF token, before calling down the middleware chain.</p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code> if you’re using <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_USE_SESSIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_USE_SESSIONS</span></code></a>.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.auth.middleware.AuthenticationMiddleware\" title=\"django.contrib.auth.middleware.AuthenticationMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AuthenticationMiddleware</span></code></a></p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code>: uses session storage.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.auth.middleware.LoginRequiredMiddleware\" title=\"django.contrib.auth.middleware.LoginRequiredMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">LoginRequiredMiddleware</span></code></a></p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">AuthenticationMiddleware</span></code>: uses user object.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.messages.middleware.MessageMiddleware\" title=\"django.contrib.messages.middleware.MessageMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MessageMiddleware</span></code></a></p>\n<p>After <code class=\"docutils literal notranslate\"><span class=\"pre\">SessionMiddleware</span></code>: can use session-based storage.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.cache.FetchFromCacheMiddleware\" title=\"django.middleware.cache.FetchFromCacheMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FetchFromCacheMiddleware</span></code></a></p>\n<p>After any middleware that modifies the <code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> header: that header is used\nto pick a value for the cache hash-key.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"#django.middleware.csp.ContentSecurityPolicyMiddleware\" title=\"django.middleware.csp.ContentSecurityPolicyMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ContentSecurityPolicyMiddleware</span></code></a></p>\n<p>Can be placed near the bottom, but ensure any middleware that accesses\n<a class=\"reference internal\" href=\"/it/6.0/ref/csp/#csp-nonce\"><span class=\"std std-ref\">csp_nonce</span></a> is positioned after it, so the nonce is\nproperly included in the response header.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/contrib/flatpages/#django.contrib.flatpages.middleware.FlatpageFallbackMiddleware\" title=\"django.contrib.flatpages.middleware.FlatpageFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FlatpageFallbackMiddleware</span></code></a></p>\n<p>Should be near the bottom as it’s a last-resort type of middleware.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/contrib/redirects/#django.contrib.redirects.middleware.RedirectFallbackMiddleware\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RedirectFallbackMiddleware</span></code></a></p>\n<p>Should be near the bottom as it’s a last-resort type of middleware.</p>\n</li>\n</ol>\n</section>","rootId":"module-django.middleware","toc":[{"title":"Available middleware","anchor":"available-middleware","children":[{"title":"Cache middleware","anchor":"module-django.middleware.cache","children":[]},{"title":"«Common» middleware","anchor":"module-django.middleware.common","children":[]},{"title":"GZip middleware","anchor":"module-django.middleware.gzip","children":[]},{"title":"Conditional GET middleware","anchor":"module-django.middleware.http","children":[]},{"title":"Locale middleware","anchor":"module-django.middleware.locale","children":[]},{"title":"Message middleware","anchor":"module-django.contrib.messages.middleware","children":[]},{"title":"Security middleware","anchor":"module-django.middleware.security","children":[{"title":"HTTP Strict Transport Security","anchor":"http-strict-transport-security","children":[]},{"title":"Referrer Policy","anchor":"referrer-policy","children":[]},{"title":"Cross-Origin Opener Policy","anchor":"cross-origin-opener-policy","children":[]},{"title":"X-Content-Type-Options: nosniff","anchor":"x-content-type-options-nosniff","children":[]},{"title":"SSL Redirect","anchor":"ssl-redirect","children":[]}]},{"title":"Session middleware","anchor":"module-django.contrib.sessions.middleware","children":[]},{"title":"Site middleware","anchor":"module-django.contrib.sites.middleware","children":[]},{"title":"Authentication middleware","anchor":"module-django.contrib.auth.middleware","children":[]},{"title":"CSRF protection middleware","anchor":"csrf-protection-middleware","children":[]},{"title":"X-Frame-Options middleware","anchor":"x-frame-options-middleware","children":[]},{"title":"Content Security Policy middleware","anchor":"content-security-policy-middleware","children":[]}]},{"title":"Middleware ordering","anchor":"middleware-ordering","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/it/6.0/ref/"}],"prev":{"docname":"ref/logging","title":"Logging","url":"/it/6.0/ref/logging/"},"next":{"docname":"ref/migration-operations","title":"Migration Operations","url":"/it/6.0/ref/migration-operations/"},"formats":{"html":"/it/6.0/ref/middleware/","markdown":"/it/6.0/ref/middleware.md","json":"/it/6.0/ref/middleware.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/ref/middleware.txt","official":"https://docs.djangoproject.com/it/6.0/ref/middleware/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}