{"title":"Content Security Policy","version":"6.1","locale":"el","docname":"ref/csp","url":"/el/6.1/ref/csp/","canonical":"https://djangodocs.dev/el/6.1/ref/csp/","summary":"Content Security Policy (CSP) is a web security standard that helps prevent content injection attacks by restricting the sources from which content can be…","html":"<h1>Content Security Policy<a class=\"heading-anchor\" href=\"#content-security-policy\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"version-note version-added\" data-version=\"6.0\" role=\"note\">\n<p class=\"version-note-title\">New in Django 6.0</p></aside>\n<p id=\"module-django.middleware.csp\">Content Security Policy (CSP) is a web security standard that helps prevent\ncontent injection attacks by restricting the sources from which content can be\nloaded. It plays an important role in a comprehensive <a class=\"reference internal\" href=\"/el/6.1/topics/security/#security-csp\"><span class=\"std std-ref\">security strategy</span></a>.</p>\n<p>For configuration instructions in a Django project, see the <a class=\"reference internal\" href=\"/el/6.1/howto/csp/#csp-config\"><span class=\"std std-ref\">Using CSP</span></a> documentation. For an HTTP guide about CSP, see the <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP\">MDN Guide on\nCSP</a>.</p>\n<section id=\"overview\">\n<span id=\"csp-overview\"></span><h2>Overview<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The <a class=\"reference external\" href=\"https://www.w3.org/TR/CSP3/\">Content-Security-Policy specification</a>\ndefines two complementary headers:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">Content-Security-Policy</code>: Enforces the CSP policy, blocking content that\nviolates the defined directives.</p></li>\n<li><p><code class=\"docutils literal notranslate\">Content-Security-Policy-Report-Only</code>: Reports CSP violations without\nblocking content, allowing for non-intrusive testing.</p></li>\n</ul>\n<p>Each policy is composed of one or more directives and their values, which\ntogether instruct the browser on how to handle specific types of content.</p>\n<p>When the <a class=\"reference internal\" href=\"/el/6.1/ref/middleware/#django.middleware.csp.ContentSecurityPolicyMiddleware\" title=\"django.middleware.csp.ContentSecurityPolicyMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">ContentSecurityPolicyMiddleware</code></a> is\nenabled, Django automatically builds and attaches the appropriate headers to\neach response based on the configured <a class=\"reference internal\" href=\"#csp-settings\"><span class=\"std std-ref\">settings</span></a>, unless\nthey have already been set by another layer.</p>\n</section>\n<section id=\"settings\">\n<span id=\"csp-settings\"></span><h2>Settings<a class=\"heading-anchor\" href=\"#settings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The <a class=\"reference internal\" href=\"/el/6.1/ref/middleware/#django.middleware.csp.ContentSecurityPolicyMiddleware\" title=\"django.middleware.csp.ContentSecurityPolicyMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">ContentSecurityPolicyMiddleware</code></a> is\nconfigured using the following settings:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/el/6.1/ref/settings/#std-setting-SECURE_CSP\"><code class=\"xref std std-setting docutils literal notranslate\">SECURE_CSP</code></a>: defines the <strong>enforced Content Security Policy</strong>.</p></li>\n<li><p><a class=\"reference internal\" href=\"/el/6.1/ref/settings/#std-setting-SECURE_CSP_REPORT_ONLY\"><code class=\"xref std std-setting docutils literal notranslate\">SECURE_CSP_REPORT_ONLY</code></a>: defines a <strong>report-only Content Security\nPolicy</strong>.</p></li>\n</ul>\n<aside class=\"admonition-these-settings-can-be-used-independently-or-together admonition\" role=\"note\">\n<p class=\"admonition-title\">These settings can be used independently or together</p>\n<ul class=\"simple\">\n<li><p>Use <a class=\"reference internal\" href=\"/el/6.1/ref/settings/#std-setting-SECURE_CSP\"><code class=\"xref std std-setting docutils literal notranslate\">SECURE_CSP</code></a> alone to enforce a policy that has already been\ntested and verified.</p></li>\n<li><p>Use <a class=\"reference internal\" href=\"/el/6.1/ref/settings/#std-setting-SECURE_CSP_REPORT_ONLY\"><code class=\"xref std std-setting docutils literal notranslate\">SECURE_CSP_REPORT_ONLY</code></a> on its own to evaluate a new policy\nwithout disrupting site behavior. This mode does not block violations, it\nonly logs them. It’s useful for testing and monitoring, but provides no\nprotection against active threats.</p></li>\n<li><p>Use <em>both</em> to maintain an enforced baseline while experimenting with\nchanges. Even for well-established policies, continuing to collect reports\ncan help detect regressions, unexpected changes in behavior, or\npotential tampering in production environments.</p></li>\n</ul>\n</aside>\n</section>\n<section id=\"policy-violation-reports\">\n<span id=\"csp-reports\"></span><h2>Policy violation reports<a class=\"heading-anchor\" href=\"#policy-violation-reports\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When a CSP violation occurs, browsers typically log details to the developer\nconsole, providing immediate feedback during development. To also receive these\nreports programmatically, the policy must include a <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy#reporting_directives\">reporting directive</a>\nsuch as <code class=\"docutils literal notranslate\">report-uri</code> that specifies where violation data should be sent.</p>\n<p>Django supports configuring these directives via the\n<a class=\"reference internal\" href=\"/el/6.1/ref/settings/#std-setting-SECURE_CSP_REPORT_ONLY\"><code class=\"xref std std-setting docutils literal notranslate\">SECURE_CSP_REPORT_ONLY</code></a> settings, but reports will only be issued by\nthe browser if the policy explicitly includes a valid reporting directive.</p>\n<p>Django does not provide built-in functionality to receive, store, or process\nviolation reports. To collect and analyze them, you must implement your own\nreporting endpoint or integrate with a third-party monitoring service.</p>\n</section>\n<section id=\"csp-constants\">\n<span id=\"id1\"></span><h2>CSP constants<a class=\"heading-anchor\" href=\"#csp-constants\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django provides predefined constants representing common CSP source expression\nkeywords such as <code class=\"docutils literal notranslate\">'self'</code>, <code class=\"docutils literal notranslate\">'none'</code>, and <code class=\"docutils literal notranslate\">'unsafe-inline'</code>. These\nconstants are intended for use in the directive values defined in the settings.</p>\n<p>They are available through the <a class=\"reference internal\" href=\"#django.utils.csp.CSP\" title=\"django.utils.csp.CSP\"><code class=\"xref py py-class docutils literal notranslate\">CSP</code></a> enum, and using\nthem is recommended over raw strings. This helps avoid common mistakes such as\ntypos, improper quoting, or inconsistent formatting, and ensures compliance\nwith the CSP specification.</p>\n<dl class=\"py class\" id=\"module-django.utils.csp\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">CSP</span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Enum providing standardized constants for common CSP source expressions.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.NONE\">\n<span class=\"sig-name descname\">NONE</span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.NONE\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\">'none'</code>. Blocks loading resources for the given directive.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.REPORT_SAMPLE\">\n<span class=\"sig-name descname\">REPORT_SAMPLE</span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.REPORT_SAMPLE\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\">'report-sample'</code>. Instructs the browser to include a sample\nof the violating code in reports. Note that this may expose sensitive\ndata.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.SELF\">\n<span class=\"sig-name descname\">SELF</span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.SELF\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\">'self'</code>. Allows loading resources from the same origin\n(same scheme, host, and port).</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.STRICT_DYNAMIC\">\n<span class=\"sig-name descname\">STRICT_DYNAMIC</span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.STRICT_DYNAMIC\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\">'strict-dynamic'</code>. Allows execution of scripts loaded by a\ntrusted script (e.g., one with a valid nonce or hash), without needing\n<code class=\"docutils literal notranslate\">'unsafe-inline'</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.UNSAFE_EVAL\">\n<span class=\"sig-name descname\">UNSAFE_EVAL</span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.UNSAFE_EVAL\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\">'unsafe-eval'</code>. Allows use of <code class=\"docutils literal notranslate\">eval()</code> and similar\nJavaScript functions. Strongly discouraged.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.UNSAFE_HASHES\">\n<span class=\"sig-name descname\">UNSAFE_HASHES</span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.UNSAFE_HASHES\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\">'unsafe-hashes'</code>. Allows inline event handlers and some\n<code class=\"docutils literal notranslate\">javascript:</code> URIs when their content hashes match a policy rule.\nRequires CSP Level 3+.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.UNSAFE_INLINE\">\n<span class=\"sig-name descname\">UNSAFE_INLINE</span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.UNSAFE_INLINE\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\">'unsafe-inline'</code>. Allows execution of inline scripts,\nstyles, and <code class=\"docutils literal notranslate\">javascript:</code> URLs. Generally discouraged, especially for\nscripts.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.WASM_UNSAFE_EVAL\">\n<span class=\"sig-name descname\">WASM_UNSAFE_EVAL</span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.WASM_UNSAFE_EVAL\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents <code class=\"docutils literal notranslate\">'wasm-unsafe-eval'</code>. Permits compilation and execution of\nWebAssembly code without enabling <code class=\"docutils literal notranslate\">'unsafe-eval'</code> for scripts.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.utils.csp.CSP.NONCE\">\n<span class=\"sig-name descname\">NONCE</span><a class=\"heading-anchor\" href=\"#django.utils.csp.CSP.NONCE\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Django-specific placeholder value (<code class=\"docutils literal notranslate\">&quot;&lt;CSP_NONCE_SENTINEL&gt;&quot;</code>) used in\n<code class=\"docutils literal notranslate\">script-src</code> or <code class=\"docutils literal notranslate\">style-src</code> directives to activate nonce-based CSP.\nThis string is replaced at runtime by the\n<a class=\"reference internal\" href=\"/el/6.1/ref/middleware/#django.middleware.csp.ContentSecurityPolicyMiddleware\" title=\"django.middleware.csp.ContentSecurityPolicyMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">ContentSecurityPolicyMiddleware</code></a> with a\nsecure, random nonce that is generated for each request. See detailed\nexplanation in <a class=\"reference internal\" href=\"#csp-nonce\"><span class=\"std std-ref\">Nonce usage</span></a>.</p>\n</dd></dl>\n\n</dd></dl>\n\n</section>\n<section id=\"module-django.views.decorators.csp\">\n<span id=\"decorators\"></span><h2>Decorators<a class=\"heading-anchor\" href=\"#module-django.views.decorators.csp\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django provides decorators to control the Content Security Policy headers on a\nper-view basis. These allow overriding or disabling the enforced or report-only\npolicy for specific views, providing fine-grained control when the global\nsettings are not sufficient. Applying these overrides fully replaces the base\nCSP: they do not merge with existing rules. They can be used alongside the\nconstants defined in <a class=\"reference internal\" href=\"#django.utils.csp.CSP\" title=\"django.utils.csp.CSP\"><code class=\"xref py py-class docutils literal notranslate\">CSP</code></a>.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Προειδοποίηση</p>\n<p>Weakening or disabling a CSP policy on any page can compromise the security\nof the entire site. Because of the «same origin» policy, an attacker could\nexploit a vulnerability on one page to access other parts of the site.</p>\n</aside>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.decorators.csp.csp_override\">\n<span class=\"sig-name descname\">csp_override</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">config)(view</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.decorators.csp.csp_override\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Overrides the <code class=\"docutils literal notranslate\">Content-Security-Policy</code> header for the decorated view\nusing directives in the same format as the <a class=\"reference internal\" href=\"/el/6.1/ref/settings/#std-setting-SECURE_CSP\"><code class=\"xref std std-setting docutils literal notranslate\">SECURE_CSP</code></a> setting.</p>\n<p>The <code class=\"docutils literal notranslate\">config</code> argument must be a mapping with the desired CSP directives.\nIf <code class=\"docutils literal notranslate\">config</code> is an empty mapping (<code class=\"docutils literal notranslate\">{}</code>), no CSP enforcement header will\nbe added to the response returned by that view, effectively disabling CSP\nfor that view.</p>\n<p>Examples:</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=\"nn\">django.http</span> <span class=\"kn\">import</span> HttpResponse\n<span class=\"kn\">from</span> <span class=\"nn\">django.utils.csp</span> <span class=\"kn\">import</span> CSP\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.decorators.csp</span> <span class=\"kn\">import</span> csp_override\n\n\n<span class=\"nd\">@csp_override</span><span class=\"p\">(</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;default-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>CSP<span class=\"o\">.</span>SELF<span class=\"p\">],</span>\n        <span class=\"s2\">&quot;img-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>CSP<span class=\"o\">.</span>SELF<span class=\"p\">,</span> <span class=\"s2\">&quot;data:&quot;</span><span class=\"p\">],</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">)</span>\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">return</span> HttpResponse<span class=\"p\">(</span><span class=\"s2\">&quot;Custom Content-Security-Policy header applied&quot;</span><span class=\"p\">)</span>\n\n\n<span class=\"nd\">@csp_override</span><span class=\"p\">({})</span>\n<span class=\"k\">def</span> <span class=\"nf\">my_other_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">return</span> HttpResponse<span class=\"p\">(</span><span class=\"s2\">&quot;No Content-Security-Policy header added&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.decorators.csp.csp_report_only_override\">\n<span class=\"sig-name descname\">csp_report_only_override</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">config)(view</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.decorators.csp.csp_report_only_override\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Overrides the <code class=\"docutils literal notranslate\">Content-Security-Policy-Report-Only</code> header for the\ndecorated view using directives in the same format as the\n<a class=\"reference internal\" href=\"/el/6.1/ref/settings/#std-setting-SECURE_CSP_REPORT_ONLY\"><code class=\"xref std std-setting docutils literal notranslate\">SECURE_CSP_REPORT_ONLY</code></a> setting.</p>\n<p>Like <a class=\"reference internal\" href=\"#django.views.decorators.csp.csp_override\" title=\"django.views.decorators.csp.csp_override\"><code class=\"xref py py-func docutils literal notranslate\">csp_override()</code></a>, the <code class=\"docutils literal notranslate\">config</code> argument must be a mapping with\nthe desired CSP directives. If <code class=\"docutils literal notranslate\">config</code> is an empty mapping (<code class=\"docutils literal notranslate\">{}</code>), no\nCSP report-only header will be added to the response returned by that view,\neffectively disabling report-only CSP for that view.</p>\n<p>Examples:</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=\"nn\">django.http</span> <span class=\"kn\">import</span> HttpResponse\n<span class=\"kn\">from</span> <span class=\"nn\">django.utils.csp</span> <span class=\"kn\">import</span> CSP\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.decorators.csp</span> <span class=\"kn\">import</span> csp_report_only_override\n\n\n<span class=\"nd\">@csp_report_only_override</span><span class=\"p\">(</span>\n    <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;default-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>CSP<span class=\"o\">.</span>SELF<span class=\"p\">],</span>\n        <span class=\"s2\">&quot;img-src&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>CSP<span class=\"o\">.</span>SELF<span class=\"p\">,</span> <span class=\"s2\">&quot;data:&quot;</span><span class=\"p\">],</span>\n        <span class=\"s2\">&quot;report-uri&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;https://mysite.com/csp-report/&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">}</span>\n<span class=\"p\">)</span>\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">return</span> HttpResponse<span class=\"p\">(</span><span class=\"s2\">&quot;Custom Content-Security-Policy-Report-Only header applied&quot;</span><span class=\"p\">)</span>\n\n\n<span class=\"nd\">@csp_report_only_override</span><span class=\"p\">({})</span>\n<span class=\"k\">def</span> <span class=\"nf\">my_other_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">return</span> HttpResponse<span class=\"p\">(</span><span class=\"s2\">&quot;No Content-Security-Policy-Report-Only header added&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<p>The examples above assume function-based views. For class-based views, see the\n<a class=\"reference internal\" href=\"/el/6.1/topics/class-based-views/intro/#id1\"><span class=\"std std-ref\">guide for decorating class-based views</span></a>.</p>\n</section>\n<section id=\"nonce-usage\">\n<span id=\"csp-nonce\"></span><h2>Nonce usage<a class=\"heading-anchor\" href=\"#nonce-usage\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A CSP nonce («number used once») is a unique, random value generated per HTTP\nresponse. Django supports nonces as a secure way to allow specific inline\n<code class=\"docutils literal notranslate\">&lt;script&gt;</code> or <code class=\"docutils literal notranslate\">&lt;style&gt;</code> elements to execute without relying on\n<code class=\"docutils literal notranslate\">'unsafe-inline'</code>.</p>\n<p>Nonces are enabled by including the special placeholder\n<a class=\"reference internal\" href=\"#django.utils.csp.CSP.NONCE\" title=\"django.utils.csp.CSP.NONCE\"><code class=\"xref py py-attr docutils literal notranslate\">NONCE</code></a> in the relevant directive(s) of your\n<a class=\"reference internal\" href=\"#csp-settings\"><span class=\"std std-ref\">CSP settings</span></a>, such as <code class=\"docutils literal notranslate\">script-src</code> or <code class=\"docutils literal notranslate\">style-src</code>.\nWhen present, the\n<a class=\"reference internal\" href=\"/el/6.1/ref/middleware/#django.middleware.csp.ContentSecurityPolicyMiddleware\" title=\"django.middleware.csp.ContentSecurityPolicyMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">ContentSecurityPolicyMiddleware</code></a>\nwill generate a nonce and insert the corresponding <code class=\"docutils literal notranslate\">nonce-&lt;value&gt;</code> source\nexpression into the CSP header.</p>\n<p>To use this nonce in templates, the\n<a class=\"reference internal\" href=\"/el/6.1/ref/templates/api/#django.template.context_processors.csp\" title=\"django.template.context_processors.csp\"><code class=\"xref py py-func docutils literal notranslate\">csp()</code></a> context processor needs to be\nenabled. It adds a <code class=\"docutils literal notranslate\">csp_nonce</code> variable to the template context.</p>\n<aside class=\"version-note version-changed\" data-version=\"6.1\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 6.1</p><p>The <a class=\"reference internal\" href=\"/el/6.1/ref/templates/builtins/#std-templatetag-csp_nonce_attr\"><code class=\"xref std std-ttag docutils literal notranslate\">csp_nonce_attr</code></a> template tag was added, including support for\nrendering <a class=\"reference internal\" href=\"/el/6.1/topics/forms/media/#django.forms.Media\" title=\"django.forms.Media\"><code class=\"xref py py-class docutils literal notranslate\">Media</code></a> objects.</p>\n</aside>\n<p>For inline <code class=\"docutils literal notranslate\">&lt;script&gt;</code> and <code class=\"docutils literal notranslate\">&lt;style&gt;</code> elements, include the nonce directly\nusing the context variable:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">script</span> <span class=\"na\">nonce</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">csp_nonce</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>\n  <span class=\"c1\">// This inline JavaScript will be allowed.</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">script</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>For external <code class=\"docutils literal notranslate\">&lt;script src=&quot;...&quot;&gt;</code> and <code class=\"docutils literal notranslate\">&lt;link rel=&quot;stylesheet&quot;&gt;</code> elements,\nuse the <a class=\"reference internal\" href=\"/el/6.1/ref/templates/builtins/#std-templatetag-csp_nonce_attr\"><code class=\"xref std std-ttag docutils literal notranslate\">csp_nonce_attr</code></a> template tag:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"p\">&lt;</span><span class=\"nt\">script</span> <span class=\"na\">src</span><span class=\"o\">=</span><span class=\"s\">&quot;/path/to/script.js&quot;</span> <span class=\"cp\">{%</span> <span class=\"k\">csp_nonce_attr</span> <span class=\"cp\">%}</span><span class=\"p\">&gt;&lt;/</span><span class=\"nt\">script</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">link</span> <span class=\"na\">rel</span><span class=\"o\">=</span><span class=\"s\">&quot;stylesheet&quot;</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;/path/to/style.css&quot;</span> <span class=\"cp\">{%</span> <span class=\"k\">csp_nonce_attr</span> <span class=\"cp\">%}</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>To render a <a class=\"reference internal\" href=\"/el/6.1/topics/forms/media/#django.forms.Media\" title=\"django.forms.Media\"><code class=\"xref py py-class docutils literal notranslate\">Media</code></a> object’s assets with the nonce\napplied, pass the object to the <a class=\"reference internal\" href=\"/el/6.1/ref/templates/builtins/#std-templatetag-csp_nonce_attr\"><code class=\"xref std std-ttag docutils literal notranslate\">csp_nonce_attr</code></a> template tag:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">csp_nonce_attr</span> <span class=\"nv\">form.media</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>The browser will only execute inline elements that include a <code class=\"docutils literal notranslate\">nonce=&lt;value&gt;</code>\nattribute matching the one specified in the <code class=\"docutils literal notranslate\">Content-Security-Policy</code> (or\n<code class=\"docutils literal notranslate\">Content-Security-Policy-Report-Only</code>) header. This mechanism provides\nfine-grained control over which inline code is allowed to run.</p>\n<p>If a template includes the CSP nonce but the policy does not include\n<a class=\"reference internal\" href=\"#django.utils.csp.CSP.NONCE\" title=\"django.utils.csp.CSP.NONCE\"><code class=\"xref py py-attr docutils literal notranslate\">NONCE</code></a>, the HTML will include a nonce attribute,\nbut the header will lack the required source expression. In this case, the\nbrowser will block the inline script or style (or report it for report-only\nconfigurations).</p>\n<section id=\"nonce-generation-and-caching\">\n<h3>Nonce generation and caching<a class=\"heading-anchor\" href=\"#nonce-generation-and-caching\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django’s nonce generation is <strong>lazy</strong>: the middleware only generates a nonce if\n<code class=\"docutils literal notranslate\">{{ csp_nonce }}</code> is accessed during template rendering. This avoids\nunnecessary work for pages that do not use nonces.</p>\n<p>However, because nonces must be unique per request, extra care is needed when\nusing full-page caching (e.g., Django’s cache middleware, CDN caching). Serving\ncached responses with previously generated nonces may result in reuse across\nusers and requests. Although such responses may still appear to work (since the\nnonce in the CSP header and HTML content match), reuse defeats the purpose of\nthe nonce and weakens security.</p>\n<p>To ensure nonce-based policies remain effective:</p>\n<ul class=\"simple\">\n<li><p>Avoid caching full responses that include <code class=\"docutils literal notranslate\">{{ csp_nonce }}</code> or\n<a class=\"reference internal\" href=\"/el/6.1/ref/templates/builtins/#std-templatetag-csp_nonce_attr\"><code class=\"xref std std-ttag docutils literal notranslate\">csp_nonce_attr</code></a>.</p></li>\n<li><p>If caching is necessary, use a strategy that injects a fresh nonce on each\nrequest, or consider refactoring your application to avoid inline scripts and\nstyles altogether.</p></li>\n</ul>\n</section>\n</section>","rootId":"content-security-policy","toc":[{"title":"Overview","anchor":"overview","children":[]},{"title":"Settings","anchor":"settings","children":[]},{"title":"Policy violation reports","anchor":"policy-violation-reports","children":[]},{"title":"CSP constants","anchor":"csp-constants","children":[]},{"title":"Decorators","anchor":"module-django.views.decorators.csp","children":[]},{"title":"Nonce usage","anchor":"nonce-usage","children":[{"title":"Nonce generation and caching","anchor":"nonce-generation-and-caching","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/el/6.1/ref/"}],"prev":{"docname":"ref/contrib/syndication","title":"The syndication feed framework","url":"/el/6.1/ref/contrib/syndication/"},"next":{"docname":"ref/csrf","title":"Cross Site Request Forgery protection","url":"/el/6.1/ref/csrf/"},"formats":{"html":"/el/6.1/ref/csp/","markdown":"/el/6.1/ref/csp.md","json":"/el/6.1/ref/csp.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/ref/csp.txt","official":"https://docs.djangoproject.com/el/6.1/ref/csp/","inVersions":["6.1","6.0"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}