{"title":"Request and response objects","version":"5.0","locale":"it","docname":"ref/request-response","url":"/it/5.0/ref/request-response/","canonical":"https://djangodocs.dev/it/5.0/ref/request-response/","summary":"Quick overview Link to this heading # Django uses request and response objects to pass state through the system. When a page is requested, Django creates an…","html":"<span id=\"request-and-response-objects\"></span><h1>Request and response objects<a class=\"heading-anchor\" href=\"#module-django.http\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"quick-overview\">\n<h2>Quick overview<a class=\"heading-anchor\" href=\"#quick-overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django uses request and response objects to pass state through the system.</p>\n<p>When a page is requested, Django creates an <a class=\"reference internal\" href=\"#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> object that\ncontains metadata about the request. Then Django loads the appropriate view,\npassing the <a class=\"reference internal\" href=\"#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> as the first argument to the view function.\nEach view is responsible for returning an <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> object.</p>\n<p>This document explains the APIs for <a class=\"reference internal\" href=\"#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> and\n<a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> objects, which are defined in the <a class=\"reference internal\" href=\"#module-django.http\" title=\"django.http: Classes dealing with HTTP requests and responses.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.http</span></code></a>\nmodule.</p>\n</section>\n<section id=\"httprequest-objects\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> objects<a class=\"heading-anchor\" href=\"#httprequest-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest\">\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\">HttpRequest</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<section id=\"attributes\">\n<span id=\"httprequest-attributes\"></span><h3>Attributes<a class=\"heading-anchor\" href=\"#attributes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>All attributes should be considered read-only, unless stated otherwise.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.scheme\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">scheme</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.scheme\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A string representing the scheme of the request (<code class=\"docutils literal notranslate\"><span class=\"pre\">http</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">https</span></code>\nusually).</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.body\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">body</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.body\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The raw HTTP request body as a bytestring. This is useful for processing\ndata in different ways than conventional HTML forms: binary images,\nXML payload etc. For processing conventional form data, use\n<a class=\"reference internal\" href=\"#django.http.HttpRequest.POST\" title=\"django.http.HttpRequest.POST\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">HttpRequest.POST</span></code></a>.</p>\n<p>You can also read from an <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> using a file-like interface with\n<a class=\"reference internal\" href=\"#django.http.HttpRequest.read\" title=\"django.http.HttpRequest.read\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">HttpRequest.read()</span></code></a> or <a class=\"reference internal\" href=\"#django.http.HttpRequest.readline\" title=\"django.http.HttpRequest.readline\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">HttpRequest.readline()</span></code></a>. Accessing\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">body</span></code> attribute <em>after</em> reading the request with either of these I/O\nstream methods will produce a <code class=\"docutils literal notranslate\"><span class=\"pre\">RawPostDataException</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.path\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">path</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.path\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A string representing the full path to the requested page, not including\nthe scheme, domain, or query string.</p>\n<p>Example: <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;/music/bands/the_beatles/&quot;</span></code></p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.path_info\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">path_info</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.path_info\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Under some web server configurations, the portion of the URL after the\nhost name is split up into a script prefix portion and a path info\nportion. The <code class=\"docutils literal notranslate\"><span class=\"pre\">path_info</span></code> attribute always contains the path info portion\nof the path, no matter what web server is being used. Using this instead\nof <a class=\"reference internal\" href=\"#django.http.HttpRequest.path\" title=\"django.http.HttpRequest.path\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">path</span></code></a> can make your code easier to move between\ntest and deployment servers.</p>\n<p>For example, if the <code class=\"docutils literal notranslate\"><span class=\"pre\">WSGIScriptAlias</span></code> for your application is set to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;/minfo&quot;</span></code>, then <code class=\"docutils literal notranslate\"><span class=\"pre\">path</span></code> might be <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;/minfo/music/bands/the_beatles/&quot;</span></code>\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">path_info</span></code> would be <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;/music/bands/the_beatles/&quot;</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.method\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">method</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.method\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A string representing the HTTP method used in the request. This is\nguaranteed to be uppercase. 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=\"k\">if</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">method</span> <span class=\"o\">==</span> <span class=\"s2\">&quot;GET&quot;</span><span class=\"p\">:</span>\n    <span class=\"n\">do_something</span><span class=\"p\">()</span>\n<span class=\"k\">elif</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">method</span> <span class=\"o\">==</span> <span class=\"s2\">&quot;POST&quot;</span><span class=\"p\">:</span>\n    <span class=\"n\">do_something_else</span><span class=\"p\">()</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.encoding\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">encoding</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.encoding\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A string representing the current encoding used to decode form submission\ndata (or <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code>, which means the <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a> setting is\nused). You can write to this attribute to change the encoding used when\naccessing the form data. Any subsequent attribute accesses (such as reading\nfrom <a class=\"reference internal\" href=\"#django.http.HttpRequest.GET\" title=\"django.http.HttpRequest.GET\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">GET</span></code></a> or <a class=\"reference internal\" href=\"#django.http.HttpRequest.POST\" title=\"django.http.HttpRequest.POST\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">POST</span></code></a>) will use the new <code class=\"docutils literal notranslate\"><span class=\"pre\">encoding</span></code> value.\nUseful if you know the form data is not in the <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a>\nencoding.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.content_type\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">content_type</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.content_type\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A string representing the MIME type of the request, parsed from the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">CONTENT_TYPE</span></code> header.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.content_params\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">content_params</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.content_params\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A dictionary of key/value parameters included in the <code class=\"docutils literal notranslate\"><span class=\"pre\">CONTENT_TYPE</span></code>\nheader.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.GET\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">GET</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.GET\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A dictionary-like object containing all given HTTP GET parameters. See the\n<a class=\"reference internal\" href=\"#django.http.QueryDict\" title=\"django.http.QueryDict\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code></a> documentation below.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.POST\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">POST</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.POST\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A dictionary-like object containing all given HTTP POST parameters,\nproviding that the request contains form data. See the\n<a class=\"reference internal\" href=\"#django.http.QueryDict\" title=\"django.http.QueryDict\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code></a> documentation below. If you need to access raw or\nnon-form data posted in the request, access this through the\n<a class=\"reference internal\" href=\"#django.http.HttpRequest.body\" title=\"django.http.HttpRequest.body\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">HttpRequest.body</span></code></a> attribute instead.</p>\n<p>It’s possible that a request can come in via POST with an empty <code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code>\ndictionary – if, say, a form is requested via the POST HTTP method but\ndoes not include form data. Therefore, you shouldn’t use <code class=\"docutils literal notranslate\"><span class=\"pre\">if</span> <span class=\"pre\">request.POST</span></code>\nto check for use of the POST method; instead, use <code class=\"docutils literal notranslate\"><span class=\"pre\">if</span> <span class=\"pre\">request.method</span> <span class=\"pre\">==</span>\n<span class=\"pre\">&quot;POST&quot;</span></code> (see <a class=\"reference internal\" href=\"#django.http.HttpRequest.method\" title=\"django.http.HttpRequest.method\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">HttpRequest.method</span></code></a>).</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code> does <em>not</em> include file-upload information. See <a class=\"reference internal\" href=\"#django.http.HttpRequest.FILES\" title=\"django.http.HttpRequest.FILES\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">FILES</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.COOKIES\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">COOKIES</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.COOKIES\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A dictionary containing all cookies. Keys and values are strings.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.FILES\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">FILES</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.FILES\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A dictionary-like object containing all uploaded files. Each key in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">FILES</span></code> is the <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> from the <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;input</span> <span class=\"pre\">type=&quot;file&quot;</span> <span class=\"pre\">name=&quot;&quot;&gt;</span></code>. Each\nvalue in <code class=\"docutils literal notranslate\"><span class=\"pre\">FILES</span></code> is an <a class=\"reference internal\" href=\"/it/5.0/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile\" title=\"django.core.files.uploadedfile.UploadedFile\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">UploadedFile</span></code></a>.</p>\n<p>See <a class=\"reference internal\" href=\"/it/5.0/topics/files/\"><span class=\"doc\">Managing files</span></a> for more information.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">FILES</span></code> will only contain data if the request method was POST and the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;form&gt;</span></code> that posted to the request had <code class=\"docutils literal notranslate\"><span class=\"pre\">enctype=&quot;multipart/form-data&quot;</span></code>.\nOtherwise, <code class=\"docutils literal notranslate\"><span class=\"pre\">FILES</span></code> will be a blank dictionary-like object.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.META\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">META</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.META\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A dictionary containing all available HTTP headers. Available headers\ndepend on the client and server, but here are some examples:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">CONTENT_LENGTH</span></code> – The length of the request body (as a string).</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">CONTENT_TYPE</span></code> – The MIME type of the request body.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_ACCEPT</span></code> – Acceptable content types for the response.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_ACCEPT_ENCODING</span></code> – Acceptable encodings for the response.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_ACCEPT_LANGUAGE</span></code> – Acceptable languages for the response.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_HOST</span></code> – The HTTP Host header sent by the client.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_REFERER</span></code> – The referring page, if any.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_USER_AGENT</span></code> – The client’s user-agent string.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">QUERY_STRING</span></code> – The query string, as a single (unparsed) string.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_ADDR</span></code> – The IP address of the client.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_HOST</span></code> – The hostname of the client.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_USER</span></code> – The user authenticated by the web server, if any.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">REQUEST_METHOD</span></code> – A string such as <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;GET&quot;</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;POST&quot;</span></code>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">SERVER_NAME</span></code> – The hostname of the server.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">SERVER_PORT</span></code> – The port of the server (as a string).</p></li>\n</ul>\n<p>With the exception of <code class=\"docutils literal notranslate\"><span class=\"pre\">CONTENT_LENGTH</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">CONTENT_TYPE</span></code>, as given\nabove, any HTTP headers in the request are converted to <code class=\"docutils literal notranslate\"><span class=\"pre\">META</span></code> keys by\nconverting all characters to uppercase, replacing any hyphens with\nunderscores and adding an <code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_</span></code> prefix to the name. So, for example, a\nheader called <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Bender</span></code> would be mapped to the <code class=\"docutils literal notranslate\"><span class=\"pre\">META</span></code> key\n<code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_X_BENDER</span></code>.</p>\n<p>Note that <a class=\"reference internal\" href=\"/it/5.0/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">runserver</span></code></a> strips all headers with underscores in the\nname, so you won’t see them in <code class=\"docutils literal notranslate\"><span class=\"pre\">META</span></code>. This prevents header-spoofing\nbased on ambiguity between underscores and dashes both being normalizing to\nunderscores in WSGI environment variables. It matches the behavior of\nweb servers like Nginx and Apache 2.4+.</p>\n<p><a class=\"reference internal\" href=\"#django.http.HttpRequest.headers\" title=\"django.http.HttpRequest.headers\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">HttpRequest.headers</span></code></a> is a simpler way to access all HTTP-prefixed\nheaders, plus <code class=\"docutils literal notranslate\"><span class=\"pre\">CONTENT_LENGTH</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">CONTENT_TYPE</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.headers\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">headers</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.headers\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A case insensitive, dict-like object that provides access to all\nHTTP-prefixed headers (plus <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code>) from\nthe request.</p>\n<p>The name of each header is stylized with title-casing (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">User-Agent</span></code>)\nwhen it’s displayed. You can access headers case-insensitively:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">headers</span>\n<span class=\"go\">{&#39;User-Agent&#39;: &#39;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6&#39;, ...}</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"s2\">&quot;User-Agent&quot;</span> <span class=\"ow\">in</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">headers</span>\n<span class=\"go\">True</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"s2\">&quot;user-agent&quot;</span> <span class=\"ow\">in</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">headers</span>\n<span class=\"go\">True</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">headers</span><span class=\"p\">[</span><span class=\"s2\">&quot;User-Agent&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">headers</span><span class=\"p\">[</span><span class=\"s2\">&quot;user-agent&quot;</span><span class=\"p\">]</span>\n<span class=\"go\">Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">headers</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s2\">&quot;User-Agent&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">headers</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s2\">&quot;user-agent&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)</span>\n</code></pre></div>\n<p>For use in, for example, Django templates, headers can also be looked up\nusing underscores in place of hyphens:</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=\"nv\">request.headers.user_agent</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.resolver_match\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">resolver_match</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.resolver_match\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>An instance of <a class=\"reference internal\" href=\"/it/5.0/ref/urlresolvers/#django.urls.ResolverMatch\" title=\"django.urls.ResolverMatch\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ResolverMatch</span></code></a> representing the\nresolved URL. This attribute is only set after URL resolving took place,\nwhich means it’s available in all views but not in middleware which are\nexecuted before URL resolving takes place (you can use it in\n<a class=\"reference internal\" href=\"/it/5.0/topics/http/middleware/#process_view\" title=\"process_view\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">process_view()</span></code></a> though).</p>\n</dd></dl>\n\n</section>\n<section id=\"attributes-set-by-application-code\">\n<h3>Attributes set by application code<a class=\"heading-anchor\" href=\"#attributes-set-by-application-code\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django doesn’t set these attributes itself but makes use of them if set by your\napplication.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.current_app\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">current_app</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.current_app\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <a class=\"reference internal\" href=\"/it/5.0/ref/templates/builtins/#std-templatetag-url\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">url</span></code></a> template tag will use its value as the <code class=\"docutils literal notranslate\"><span class=\"pre\">current_app</span></code>\nargument to <a class=\"reference internal\" href=\"/it/5.0/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">reverse()</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.urlconf\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">urlconf</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.urlconf\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This will be used as the root URLconf for the current request, overriding\nthe <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-ROOT_URLCONF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ROOT_URLCONF</span></code></a> setting. See\n<a class=\"reference internal\" href=\"/it/5.0/topics/http/urls/#how-django-processes-a-request\"><span class=\"std std-ref\">How Django processes a request</span></a> for details.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">urlconf</span></code> can be set to <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> to revert any changes made by previous\nmiddleware and return to using the <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-ROOT_URLCONF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ROOT_URLCONF</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.exception_reporter_filter\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">exception_reporter_filter</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.exception_reporter_filter\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This will be used instead of <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-DEFAULT_EXCEPTION_REPORTER_FILTER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_EXCEPTION_REPORTER_FILTER</span></code></a>\nfor the current request. See <a class=\"reference internal\" href=\"/it/5.0/howto/error-reporting/#custom-error-reports\"><span class=\"std std-ref\">Rapporti di errore personalizzati</span></a> for details.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.exception_reporter_class\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">exception_reporter_class</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.exception_reporter_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This will be used instead of <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-DEFAULT_EXCEPTION_REPORTER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_EXCEPTION_REPORTER</span></code></a> for the\ncurrent request. See <a class=\"reference internal\" href=\"/it/5.0/howto/error-reporting/#custom-error-reports\"><span class=\"std std-ref\">Rapporti di errore personalizzati</span></a> for details.</p>\n</dd></dl>\n\n</section>\n<section id=\"attributes-set-by-middleware\">\n<h3>Attributes set by middleware<a class=\"heading-anchor\" href=\"#attributes-set-by-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Some of the middleware included in Django’s contrib apps set attributes on the\nrequest. If you don’t see the attribute on a request, be sure the appropriate\nmiddleware class is listed in <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a>.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.session\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">session</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.session\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>From the <a class=\"reference internal\" href=\"/it/5.0/ref/middleware/#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>: A\nreadable and writable, dictionary-like object that represents the current\nsession.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.site\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">site</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.site\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>From the <a class=\"reference internal\" href=\"/it/5.0/ref/middleware/#django.contrib.sites.middleware.CurrentSiteMiddleware\" title=\"django.contrib.sites.middleware.CurrentSiteMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CurrentSiteMiddleware</span></code></a>:\nAn instance of <a class=\"reference internal\" href=\"/it/5.0/ref/contrib/sites/#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Site</span></code></a> or\n<a class=\"reference internal\" href=\"/it/5.0/ref/contrib/sites/#django.contrib.sites.requests.RequestSite\" title=\"django.contrib.sites.requests.RequestSite\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RequestSite</span></code></a> as returned by\n<a class=\"reference internal\" href=\"/it/5.0/ref/contrib/sites/#django.contrib.sites.shortcuts.get_current_site\" title=\"django.contrib.sites.shortcuts.get_current_site\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">get_current_site()</span></code></a>\nrepresenting the current site.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.user\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">user</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.user\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>From the <a class=\"reference internal\" href=\"/it/5.0/ref/middleware/#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>:\nAn instance of <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-AUTH_USER_MODEL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">AUTH_USER_MODEL</span></code></a> representing the currently\nlogged-in user. If the user isn’t currently logged in, <code class=\"docutils literal notranslate\"><span class=\"pre\">user</span></code> will be set\nto an instance of <a class=\"reference internal\" href=\"/it/5.0/ref/contrib/auth/#django.contrib.auth.models.AnonymousUser\" title=\"django.contrib.auth.models.AnonymousUser\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AnonymousUser</span></code></a>. You\ncan tell them apart with\n<a class=\"reference internal\" href=\"/it/5.0/ref/contrib/auth/#django.contrib.auth.models.User.is_authenticated\" title=\"django.contrib.auth.models.User.is_authenticated\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">is_authenticated</span></code></a>, like so:</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=\"k\">if</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">user</span><span class=\"o\">.</span><span class=\"n\">is_authenticated</span><span class=\"p\">:</span>\n    <span class=\"o\">...</span>  <span class=\"c1\"># Do something for logged-in users.</span>\n<span class=\"k\">else</span><span class=\"p\">:</span>\n    <span class=\"o\">...</span>  <span class=\"c1\"># Do something for anonymous users.</span>\n</code></pre></div>\n<p>The <a class=\"reference internal\" href=\"#django.http.HttpRequest.auser\" title=\"django.http.HttpRequest.auser\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">auser()</span></code></a> method does the same thing but can be used from async\ncontexts.</p>\n</dd></dl>\n\n</section>\n<section id=\"methods\">\n<h3>Methods<a class=\"heading-anchor\" href=\"#methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.auser\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">auser</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.auser\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"5.0\">\n<p class=\"version-note-title\">New in Django 5.0</p></aside>\n<p>From the <a class=\"reference internal\" href=\"/it/5.0/ref/middleware/#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>:\nCoroutine. Returns an instance of <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-AUTH_USER_MODEL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">AUTH_USER_MODEL</span></code></a> representing\nthe currently logged-in user. If the user isn’t currently logged in,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">auser</span></code> will return an instance of\n<a class=\"reference internal\" href=\"/it/5.0/ref/contrib/auth/#django.contrib.auth.models.AnonymousUser\" title=\"django.contrib.auth.models.AnonymousUser\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AnonymousUser</span></code></a>. This is similar to the\n<a class=\"reference internal\" href=\"#django.http.HttpRequest.user\" title=\"django.http.HttpRequest.user\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">user</span></code></a> attribute but it works in async contexts.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.get_host\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_host</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.get_host\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the originating host of the request using information from the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_X_FORWARDED_HOST</span></code> (if <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-USE_X_FORWARDED_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_X_FORWARDED_HOST</span></code></a> is enabled)\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_HOST</span></code> headers, in that order. If they don’t provide a value,\nthe method uses a combination of <code class=\"docutils literal notranslate\"><span class=\"pre\">SERVER_NAME</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">SERVER_PORT</span></code> as\ndetailed in <span class=\"target\" id=\"index-0\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-3333/\"><strong>PEP 3333</strong></a>.</p>\n<p>Example: <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;127.0.0.1:8000&quot;</span></code></p>\n<p>Raises <code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.exceptions.DisallowedHost</span></code> if the host is not in\n<a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-ALLOWED_HOSTS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ALLOWED_HOSTS</span></code></a> or the domain name is invalid according to\n<span class=\"target\" id=\"index-1\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc1034.html\"><strong>RFC 1034</strong></a>/<span class=\"target\" id=\"index-2\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc1035.html\"><strong>1035</strong></a>.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>The <a class=\"reference internal\" href=\"#django.http.HttpRequest.get_host\" title=\"django.http.HttpRequest.get_host\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">get_host()</span></code></a> method fails when the host is\nbehind multiple proxies. One solution is to use middleware to rewrite\nthe proxy headers, as in the following 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=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MultipleProxyMiddleware</span><span class=\"p\">:</span>\n    <span class=\"n\">FORWARDED_FOR_FIELDS</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n        <span class=\"s2\">&quot;HTTP_X_FORWARDED_FOR&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;HTTP_X_FORWARDED_HOST&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;HTTP_X_FORWARDED_SERVER&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">]</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">get_response</span><span class=\"p\">):</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">get_response</span> <span class=\"o\">=</span> <span class=\"n\">get_response</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__call__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">request</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Rewrites the proxy headers so that only the most</span>\n<span class=\"sd\">        recent proxy is used.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">for</span> <span class=\"n\">field</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">FORWARDED_FOR_FIELDS</span><span class=\"p\">:</span>\n            <span class=\"k\">if</span> <span class=\"n\">field</span> <span class=\"ow\">in</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">META</span><span class=\"p\">:</span>\n                <span class=\"k\">if</span> <span class=\"s2\">&quot;,&quot;</span> <span class=\"ow\">in</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">META</span><span class=\"p\">[</span><span class=\"n\">field</span><span class=\"p\">]:</span>\n                    <span class=\"n\">parts</span> <span class=\"o\">=</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">META</span><span class=\"p\">[</span><span class=\"n\">field</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"s2\">&quot;,&quot;</span><span class=\"p\">)</span>\n                    <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">META</span><span class=\"p\">[</span><span class=\"n\">field</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">parts</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">strip</span><span class=\"p\">()</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">get_response</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This middleware should be positioned before any other middleware that\nrelies on the value of <a class=\"reference internal\" href=\"#django.http.HttpRequest.get_host\" title=\"django.http.HttpRequest.get_host\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">get_host()</span></code></a> – for instance,\n<a class=\"reference internal\" href=\"/it/5.0/ref/middleware/#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> or\n<a class=\"reference internal\" href=\"/it/5.0/ref/middleware/#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</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.get_port\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_port</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.get_port\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the originating port of the request using information from the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_X_FORWARDED_PORT</span></code> (if <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-USE_X_FORWARDED_PORT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_X_FORWARDED_PORT</span></code></a> is enabled)\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">SERVER_PORT</span></code> <code class=\"docutils literal notranslate\"><span class=\"pre\">META</span></code> variables, in that order.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.get_full_path\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_full_path</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.get_full_path\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the <code class=\"docutils literal notranslate\"><span class=\"pre\">path</span></code>, plus an appended query string, if applicable.</p>\n<p>Example: <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;/music/bands/the_beatles/?print=true&quot;</span></code></p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.get_full_path_info\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_full_path_info</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.get_full_path_info\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Like <a class=\"reference internal\" href=\"#django.http.HttpRequest.get_full_path\" title=\"django.http.HttpRequest.get_full_path\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">get_full_path()</span></code></a>, but uses <a class=\"reference internal\" href=\"#django.http.HttpRequest.path_info\" title=\"django.http.HttpRequest.path_info\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">path_info</span></code></a> instead of\n<a class=\"reference internal\" href=\"#django.http.HttpRequest.path\" title=\"django.http.HttpRequest.path\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">path</span></code></a>.</p>\n<p>Example: <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;/minfo/music/bands/the_beatles/?print=true&quot;</span></code></p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.build_absolute_uri\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">build_absolute_uri</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">location</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.build_absolute_uri\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the absolute URI form of <code class=\"docutils literal notranslate\"><span class=\"pre\">location</span></code>. If no location is provided,\nthe location will be set to <code class=\"docutils literal notranslate\"><span class=\"pre\">request.get_full_path()</span></code>.</p>\n<p>If the location is already an absolute URI, it will not be altered.\nOtherwise the absolute URI is built using the server variables available in\nthis request. For example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">build_absolute_uri</span><span class=\"p\">()</span>\n<span class=\"go\">&#39;https://example.com/music/bands/the_beatles/?print=true&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">build_absolute_uri</span><span class=\"p\">(</span><span class=\"s2\">&quot;/bands/&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&#39;https://example.com/bands/&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">build_absolute_uri</span><span class=\"p\">(</span><span class=\"s2\">&quot;https://example2.com/bands/&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&#39;https://example2.com/bands/&#39;</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>Mixing HTTP and HTTPS on the same site is discouraged, therefore\n<a class=\"reference internal\" href=\"#django.http.HttpRequest.build_absolute_uri\" title=\"django.http.HttpRequest.build_absolute_uri\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">build_absolute_uri()</span></code></a> will always generate an\nabsolute URI with the same scheme the current request has. If you need\nto redirect users to HTTPS, it’s best to let your web server redirect\nall HTTP traffic to HTTPS.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.get_signed_cookie\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get_signed_cookie</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">default</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">RAISE_ERROR</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">salt</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">''</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">max_age</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.get_signed_cookie\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a cookie value for a signed cookie, or raises a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.signing.BadSignature</span></code> exception if the signature is\nno longer valid. If you provide the <code class=\"docutils literal notranslate\"><span class=\"pre\">default</span></code> argument the exception\nwill be suppressed and that default value will be returned instead.</p>\n<p>The optional <code class=\"docutils literal notranslate\"><span class=\"pre\">salt</span></code> argument can be used to provide extra protection\nagainst brute force attacks on your secret key. If supplied, the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">max_age</span></code> argument will be checked against the signed timestamp\nattached to the cookie value to ensure the cookie is not older than\n<code class=\"docutils literal notranslate\"><span class=\"pre\">max_age</span></code> seconds.</p>\n<p>For example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">get_signed_cookie</span><span class=\"p\">(</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&#39;Tony&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">get_signed_cookie</span><span class=\"p\">(</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">,</span> <span class=\"n\">salt</span><span class=\"o\">=</span><span class=\"s2\">&quot;name-salt&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&#39;Tony&#39; # assuming cookie was set using the same salt</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">get_signed_cookie</span><span class=\"p\">(</span><span class=\"s2\">&quot;nonexistent-cookie&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">KeyError: &#39;nonexistent-cookie&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">get_signed_cookie</span><span class=\"p\">(</span><span class=\"s2\">&quot;nonexistent-cookie&quot;</span><span class=\"p\">,</span> <span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"go\">False</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">get_signed_cookie</span><span class=\"p\">(</span><span class=\"s2\">&quot;cookie-that-was-tampered-with&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">BadSignature: ...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">get_signed_cookie</span><span class=\"p\">(</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">,</span> <span class=\"n\">max_age</span><span class=\"o\">=</span><span class=\"mi\">60</span><span class=\"p\">)</span>\n<span class=\"go\">SignatureExpired: Signature age 1677.3839159 &gt; 60 seconds</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">get_signed_cookie</span><span class=\"p\">(</span><span class=\"s2\">&quot;name&quot;</span><span class=\"p\">,</span> <span class=\"kc\">False</span><span class=\"p\">,</span> <span class=\"n\">max_age</span><span class=\"o\">=</span><span class=\"mi\">60</span><span class=\"p\">)</span>\n<span class=\"go\">False</span>\n</code></pre></div>\n<p>See <a class=\"reference internal\" href=\"/it/5.0/topics/signing/\"><span class=\"doc\">cryptographic signing</span></a> for more information.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.is_secure\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">is_secure</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.is_secure\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> if the request is secure; that is, if it was made with\nHTTPS.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.accepts\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">accepts</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">mime_type</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.accepts\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> if the request <code class=\"docutils literal notranslate\"><span class=\"pre\">Accept</span></code> header matches the <code class=\"docutils literal notranslate\"><span class=\"pre\">mime_type</span></code>\nargument:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">accepts</span><span class=\"p\">(</span><span class=\"s2\">&quot;text/html&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">True</span>\n</code></pre></div>\n<p>Most browsers send <code class=\"docutils literal notranslate\"><span class=\"pre\">Accept:</span> <span class=\"pre\">*/*</span></code> by default, so this would return\n<code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> for all content types. Setting an explicit <code class=\"docutils literal notranslate\"><span class=\"pre\">Accept</span></code> header in\nAPI requests can be useful for returning a different content type for those\nconsumers only. See <a class=\"reference internal\" href=\"/it/5.0/topics/class-based-views/generic-editing/#content-negotiation-example\"><span class=\"std std-ref\">Content negotiation example</span></a> of using\n<code class=\"docutils literal notranslate\"><span class=\"pre\">accepts()</span></code> to return different content to API consumers.</p>\n<p>If a response varies depending on the content of the <code class=\"docutils literal notranslate\"><span class=\"pre\">Accept</span></code> header and\nyou are using some form of caching like Django’s <a class=\"reference internal\" href=\"/it/5.0/ref/middleware/#module-django.middleware.cache\" title=\"django.middleware.cache: Middleware for the site-wide cache.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">cache</span> <span class=\"pre\">middleware</span></code></a>, you should decorate the view with\n<a class=\"reference internal\" href=\"/it/5.0/topics/http/decorators/#django.views.decorators.vary.vary_on_headers\" title=\"django.views.decorators.vary.vary_on_headers\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">vary_on_headers('Accept')</span></code></a> so that the responses are\nproperly cached.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.read\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">read</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">size</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.read\"><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 method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.readline\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">readline</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.readline\"><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 method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.readlines\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">readlines</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.readlines\"><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 method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpRequest.__iter__\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpRequest.</span></span><span class=\"sig-name descname\"><span class=\"pre\">__iter__</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpRequest.__iter__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Methods implementing a file-like interface for reading from an\n<code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> instance. This makes it possible to consume an incoming\nrequest in a streaming fashion. A common use-case would be to process a\nbig XML payload with an iterative parser without constructing a whole\nXML tree in memory.</p>\n<p>Given this standard interface, an <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code> instance can be\npassed directly to an XML parser such as\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ElementTree</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=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">xml.etree.ElementTree</span><span class=\"w\"> </span><span class=\"k\">as</span><span class=\"w\"> </span><span class=\"nn\">ET</span>\n\n<span class=\"k\">for</span> <span class=\"n\">element</span> <span class=\"ow\">in</span> <span class=\"n\">ET</span><span class=\"o\">.</span><span class=\"n\">iterparse</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"n\">process</span><span class=\"p\">(</span><span class=\"n\">element</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"querydict-objects\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code> objects<a class=\"heading-anchor\" href=\"#querydict-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict\">\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\">QueryDict</span></span><a class=\"heading-anchor\" href=\"#django.http.QueryDict\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>In an <a class=\"reference internal\" href=\"#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> object, the <a class=\"reference internal\" href=\"#django.http.HttpRequest.GET\" title=\"django.http.HttpRequest.GET\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">GET</span></code></a> and\n<a class=\"reference internal\" href=\"#django.http.HttpRequest.POST\" title=\"django.http.HttpRequest.POST\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">POST</span></code></a> attributes are instances of <code class=\"docutils literal notranslate\"><span class=\"pre\">django.http.QueryDict</span></code>,\na dictionary-like class customized to deal with multiple values for the same\nkey. This is necessary because some HTML form elements, notably\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;select</span> <span class=\"pre\">multiple&gt;</span></code>, pass multiple values for the same key.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code>s at <code class=\"docutils literal notranslate\"><span class=\"pre\">request.POST</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">request.GET</span></code> will be immutable\nwhen accessed in a normal request/response cycle. To get a mutable version you\nneed to use <a class=\"reference internal\" href=\"#django.http.QueryDict.copy\" title=\"django.http.QueryDict.copy\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">QueryDict.copy()</span></code></a>.</p>\n<section id=\"id1\">\n<h3>Methods<a class=\"heading-anchor\" href=\"#id1\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference internal\" href=\"#django.http.QueryDict\" title=\"django.http.QueryDict\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code></a> implements all the standard dictionary methods because it’s\na subclass of dictionary. Exceptions are outlined here:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.__init__\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">__init__</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">query_string</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">mutable</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">encoding</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.__init__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Instantiates a <code class=\"docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code> object based on <code class=\"docutils literal notranslate\"><span class=\"pre\">query_string</span></code>.</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">QueryDict</span><span class=\"p\">(</span><span class=\"s2\">&quot;a=1&amp;a=2&amp;c=3&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;QueryDict: {&#39;a&#39;: [&#39;1&#39;, &#39;2&#39;], &#39;c&#39;: [&#39;3&#39;]}&gt;</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">query_string</span></code> is not passed in, the resulting <code class=\"docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code> will be\nempty (it will have no keys or values).</p>\n<p>Most <code class=\"docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code>s you encounter, and in particular those at\n<code class=\"docutils literal notranslate\"><span class=\"pre\">request.POST</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">request.GET</span></code>, will be immutable. If you are\ninstantiating one yourself, you can make it mutable by passing\n<code class=\"docutils literal notranslate\"><span class=\"pre\">mutable=True</span></code> to its <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__()</span></code>.</p>\n<p>Strings for setting both keys and values will be converted from <code class=\"docutils literal notranslate\"><span class=\"pre\">encoding</span></code>\nto <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code>. If <code class=\"docutils literal notranslate\"><span class=\"pre\">encoding</span></code> is not set, it defaults to\n<a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.fromkeys\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">classmethod</span></span><span class=\"w\"> </span></em><span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">fromkeys</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">iterable</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">value</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">''</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">mutable</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">encoding</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.fromkeys\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates a new <code class=\"docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code> with keys from <code class=\"docutils literal notranslate\"><span class=\"pre\">iterable</span></code> and each value\nequal to <code class=\"docutils literal notranslate\"><span class=\"pre\">value</span></code>. For example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">QueryDict</span><span class=\"o\">.</span><span class=\"n\">fromkeys</span><span class=\"p\">([</span><span class=\"s2\">&quot;a&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;a&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;b&quot;</span><span class=\"p\">],</span> <span class=\"n\">value</span><span class=\"o\">=</span><span class=\"s2\">&quot;val&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;QueryDict: {&#39;a&#39;: [&#39;val&#39;, &#39;val&#39;], &#39;b&#39;: [&#39;val&#39;]}&gt;</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.__getitem__\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">__getitem__</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.__getitem__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the value for the given key. If the key has more than one value,\nit returns the last value. Raises\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.datastructures.MultiValueDictKeyError</span></code> if the key does not\nexist. (This is a subclass of Python’s standard <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#KeyError\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">KeyError</span></code></a>, so you can\nstick to catching <code class=\"docutils literal notranslate\"><span class=\"pre\">KeyError</span></code>.)</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.__setitem__\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">__setitem__</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">value</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.__setitem__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Sets the given key to <code class=\"docutils literal notranslate\"><span class=\"pre\">[value]</span></code> (a list whose single element is\n<code class=\"docutils literal notranslate\"><span class=\"pre\">value</span></code>). Note that this, as other dictionary functions that have side\neffects, can only be called on a mutable <code class=\"docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code> (such as one that\nwas created via <a class=\"reference internal\" href=\"#django.http.QueryDict.copy\" title=\"django.http.QueryDict.copy\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">QueryDict.copy()</span></code></a>).</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.__contains__\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">__contains__</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.__contains__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> if the given key is set. This lets you do, e.g., <code class=\"docutils literal notranslate\"><span class=\"pre\">if</span> <span class=\"pre\">&quot;foo&quot;</span>\n<span class=\"pre\">in</span> <span class=\"pre\">request.GET</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.get\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">default</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.get\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Uses the same logic as <a class=\"reference internal\" href=\"#django.http.QueryDict.__getitem__\" title=\"django.http.QueryDict.__getitem__\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__getitem__()</span></code></a>, with a hook for returning a\ndefault value if the key doesn’t exist.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.setdefault\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">setdefault</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">default</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.setdefault\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Like <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict.setdefault\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">dict.setdefault()</span></code></a>, except it uses <a class=\"reference internal\" href=\"#django.http.QueryDict.__setitem__\" title=\"django.http.QueryDict.__setitem__\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__setitem__()</span></code></a> internally.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.update\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">update</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">other_dict</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.update\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Takes either a <code class=\"docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code> or a dictionary. Like <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict.update\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">dict.update()</span></code></a>,\nexcept it <em>appends</em> to the current dictionary items rather than replacing\nthem. For example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">QueryDict</span><span class=\"p\">(</span><span class=\"s2\">&quot;a=1&quot;</span><span class=\"p\">,</span> <span class=\"n\">mutable</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">update</span><span class=\"p\">({</span><span class=\"s2\">&quot;a&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;2&quot;</span><span class=\"p\">})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">getlist</span><span class=\"p\">(</span><span class=\"s2\">&quot;a&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">[&#39;1&#39;, &#39;2&#39;]</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"p\">[</span><span class=\"s2\">&quot;a&quot;</span><span class=\"p\">]</span>  <span class=\"c1\"># returns the last</span>\n<span class=\"go\">&#39;2&#39;</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.items\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">items</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.items\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Like <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict.items\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">dict.items()</span></code></a>, except this uses the same last-value logic as\n<a class=\"reference internal\" href=\"#django.http.QueryDict.__getitem__\" title=\"django.http.QueryDict.__getitem__\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__getitem__()</span></code></a> and returns an iterator object instead of a view object.\nFor example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">QueryDict</span><span class=\"p\">(</span><span class=\"s2\">&quot;a=1&amp;a=2&amp;a=3&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">list</span><span class=\"p\">(</span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">items</span><span class=\"p\">())</span>\n<span class=\"go\">[(&#39;a&#39;, &#39;3&#39;)]</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.values\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">values</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.values\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Like <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict.values\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">dict.values()</span></code></a>, except this uses the same last-value logic as\n<a class=\"reference internal\" href=\"#django.http.QueryDict.__getitem__\" title=\"django.http.QueryDict.__getitem__\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__getitem__()</span></code></a> and returns an iterator instead of a view object. For\nexample:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">QueryDict</span><span class=\"p\">(</span><span class=\"s2\">&quot;a=1&amp;a=2&amp;a=3&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">list</span><span class=\"p\">(</span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">values</span><span class=\"p\">())</span>\n<span class=\"go\">[&#39;3&#39;]</span>\n</code></pre></div>\n</dd></dl>\n\n<p>In addition, <code class=\"docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code> has the following methods:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.copy\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">copy</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.copy\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a copy of the object using <a class=\"reference external\" href=\"https://docs.python.org/3/library/copy.html#copy.deepcopy\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">copy.deepcopy()</span></code></a>. This copy will\nbe mutable even if the original was not.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.getlist\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">getlist</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">default</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.getlist\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a list of the data with the requested key. Returns an empty list if\nthe key doesn’t exist and <code class=\"docutils literal notranslate\"><span class=\"pre\">default</span></code> is <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code>. It’s guaranteed to\nreturn a list unless the default value provided isn’t a list.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.setlist\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">setlist</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">list_</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.setlist\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Sets the given key to <code class=\"docutils literal notranslate\"><span class=\"pre\">list_</span></code> (unlike <a class=\"reference internal\" href=\"#django.http.QueryDict.__setitem__\" title=\"django.http.QueryDict.__setitem__\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__setitem__()</span></code></a>).</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.appendlist\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">appendlist</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">item</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.appendlist\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Appends an item to the internal list associated with key.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.setlistdefault\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">setlistdefault</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">default_list</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.setlistdefault\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Like <a class=\"reference internal\" href=\"#django.http.QueryDict.setdefault\" title=\"django.http.QueryDict.setdefault\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">setdefault()</span></code></a>, except it takes a list of values instead of a\nsingle value.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.lists\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">lists</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.lists\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Like <a class=\"reference internal\" href=\"#django.http.QueryDict.items\" title=\"django.http.QueryDict.items\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">items()</span></code></a>, except it includes all values, as a list, for each\nmember of the dictionary. For example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">QueryDict</span><span class=\"p\">(</span><span class=\"s2\">&quot;a=1&amp;a=2&amp;a=3&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">lists</span><span class=\"p\">()</span>\n<span class=\"go\">[(&#39;a&#39;, [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;])]</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.pop\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">pop</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.pop\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a list of values for the given key and removes them from the\ndictionary. Raises <code class=\"docutils literal notranslate\"><span class=\"pre\">KeyError</span></code> if the key does not exist. For example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">QueryDict</span><span class=\"p\">(</span><span class=\"s2\">&quot;a=1&amp;a=2&amp;a=3&quot;</span><span class=\"p\">,</span> <span class=\"n\">mutable</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">pop</span><span class=\"p\">(</span><span class=\"s2\">&quot;a&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">[&#39;1&#39;, &#39;2&#39;, &#39;3&#39;]</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.popitem\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">popitem</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.popitem\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Removes an arbitrary member of the dictionary (since there’s no concept\nof ordering), and returns a two value tuple containing the key and a list\nof all values for the key. Raises <code class=\"docutils literal notranslate\"><span class=\"pre\">KeyError</span></code> when called on an empty\ndictionary. For example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">QueryDict</span><span class=\"p\">(</span><span class=\"s2\">&quot;a=1&amp;a=2&amp;a=3&quot;</span><span class=\"p\">,</span> <span class=\"n\">mutable</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">popitem</span><span class=\"p\">()</span>\n<span class=\"go\">(&#39;a&#39;, [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;])</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.dict\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">dict</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.dict\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a <code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code> representation of <code class=\"docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code>. For every (key, list)\npair in <code class=\"docutils literal notranslate\"><span class=\"pre\">QueryDict</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code> will have (key, item), where item is one\nelement of the list, using the same logic as <a class=\"reference internal\" href=\"#django.http.QueryDict.__getitem__\" title=\"django.http.QueryDict.__getitem__\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">QueryDict.__getitem__()</span></code></a>:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">QueryDict</span><span class=\"p\">(</span><span class=\"s2\">&quot;a=1&amp;a=3&amp;a=5&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">dict</span><span class=\"p\">()</span>\n<span class=\"go\">{&#39;a&#39;: &#39;5&#39;}</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.QueryDict.urlencode\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">QueryDict.</span></span><span class=\"sig-name descname\"><span class=\"pre\">urlencode</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">safe</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.QueryDict.urlencode\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a string of the data in query string format. For example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">QueryDict</span><span class=\"p\">(</span><span class=\"s2\">&quot;a=2&amp;b=3&amp;b=5&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">urlencode</span><span class=\"p\">()</span>\n<span class=\"go\">&#39;a=2&amp;b=3&amp;b=5&#39;</span>\n</code></pre></div>\n<p>Use the <code class=\"docutils literal notranslate\"><span class=\"pre\">safe</span></code> parameter to pass characters which don’t require encoding.\nFor example:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">QueryDict</span><span class=\"p\">(</span><span class=\"n\">mutable</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"p\">[</span><span class=\"s2\">&quot;next&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;/a&amp;b/&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">urlencode</span><span class=\"p\">(</span><span class=\"n\">safe</span><span class=\"o\">=</span><span class=\"s2\">&quot;/&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">&#39;next=/a%26b/&#39;</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"httpresponse-objects\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code> objects<a class=\"heading-anchor\" href=\"#httpresponse-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse\">\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\">HttpResponse</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>In contrast to <a class=\"reference internal\" href=\"#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> objects, which are created automatically by\nDjango, <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> objects are your responsibility. Each view you\nwrite is responsible for instantiating, populating, and returning an\n<a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a>.</p>\n<p>The <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> class lives in the <a class=\"reference internal\" href=\"#module-django.http\" title=\"django.http: Classes dealing with HTTP requests and responses.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.http</span></code></a> module.</p>\n<section id=\"usage\">\n<h3>Usage<a class=\"heading-anchor\" href=\"#usage\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"passing-strings\">\n<h4>Passing strings<a class=\"heading-anchor\" href=\"#passing-strings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Typical usage is to pass the contents of the page, as a string, bytestring,\nor <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#memoryview\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">memoryview</span></code></a>, to the <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> constructor:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HttpResponse</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"s2\">&quot;Here&#39;s the text of the web page.&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"s2\">&quot;Text only, please.&quot;</span><span class=\"p\">,</span> <span class=\"n\">content_type</span><span class=\"o\">=</span><span class=\"s2\">&quot;text/plain&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"sa\">b</span><span class=\"s2\">&quot;Bytestrings are also accepted.&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"nb\">memoryview</span><span class=\"p\">(</span><span class=\"sa\">b</span><span class=\"s2\">&quot;Memoryview as well.&quot;</span><span class=\"p\">))</span>\n</code></pre></div>\n<p>But if you want to add content incrementally, you can use <code class=\"docutils literal notranslate\"><span class=\"pre\">response</span></code> as a\nfile-like object:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">HttpResponse</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">write</span><span class=\"p\">(</span><span class=\"s2\">&quot;&lt;p&gt;Here&#39;s the text of the web page.&lt;/p&gt;&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">write</span><span class=\"p\">(</span><span class=\"s2\">&quot;&lt;p&gt;Here&#39;s another paragraph.&lt;/p&gt;&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"passing-iterators\">\n<h4>Passing iterators<a class=\"heading-anchor\" href=\"#passing-iterators\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Finally, you can pass <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code> an iterator rather than strings.\n<code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code> will consume the iterator immediately, store its content as a\nstring, and discard it. Objects with a <code class=\"docutils literal notranslate\"><span class=\"pre\">close()</span></code> method such as files and\ngenerators are immediately closed.</p>\n<p>If you need the response to be streamed from the iterator to the client, you\nmust use the <a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a> class instead.</p>\n</section>\n<section id=\"setting-header-fields\">\n<span id=\"id2\"></span><h4>Setting header fields<a class=\"heading-anchor\" href=\"#setting-header-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>To set or remove a header field in your response, use\n<a class=\"reference internal\" href=\"#django.http.HttpResponse.headers\" title=\"django.http.HttpResponse.headers\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">HttpResponse.headers</span></code></a>:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">HttpResponse</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">headers</span><span class=\"p\">[</span><span class=\"s2\">&quot;Age&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"mi\">120</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">del</span> <span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">headers</span><span class=\"p\">[</span><span class=\"s2\">&quot;Age&quot;</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>You can also manipulate headers by treating your response like a dictionary:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">HttpResponse</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span><span class=\"p\">[</span><span class=\"s2\">&quot;Age&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"mi\">120</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">del</span> <span class=\"n\">response</span><span class=\"p\">[</span><span class=\"s2\">&quot;Age&quot;</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>This proxies to <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse.headers</span></code>, and is the original interface offered\nby <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code>.</p>\n<p>When using this interface, unlike a dictionary, <code class=\"docutils literal notranslate\"><span class=\"pre\">del</span></code> doesn’t raise\n<code class=\"docutils literal notranslate\"><span class=\"pre\">KeyError</span></code> if the header field doesn’t exist.</p>\n<p>You can also set headers on instantiation:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"n\">headers</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;Age&quot;</span><span class=\"p\">:</span> <span class=\"mi\">120</span><span class=\"p\">})</span>\n</code></pre></div>\n<p>For setting the <code class=\"docutils literal notranslate\"><span class=\"pre\">Cache-Control</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">Vary</span></code> header fields, it is recommended\nto use the <a class=\"reference internal\" href=\"/it/5.0/ref/utils/#django.utils.cache.patch_cache_control\" title=\"django.utils.cache.patch_cache_control\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">patch_cache_control()</span></code></a> and\n<a class=\"reference internal\" href=\"/it/5.0/ref/utils/#django.utils.cache.patch_vary_headers\" title=\"django.utils.cache.patch_vary_headers\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">patch_vary_headers()</span></code></a> methods from\n<a class=\"reference internal\" href=\"/it/5.0/ref/utils/#module-django.utils.cache\" title=\"django.utils.cache: Helper functions for controlling caching.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.utils.cache</span></code></a>, since these fields can have multiple, comma-separated\nvalues. The «patch» methods ensure that other values, e.g. added by a\nmiddleware, are not removed.</p>\n<p>HTTP header fields cannot contain newlines. An attempt to set a header field\ncontaining a newline character (CR or LF) will raise <code class=\"docutils literal notranslate\"><span class=\"pre\">BadHeaderError</span></code></p>\n</section>\n<section id=\"telling-the-browser-to-treat-the-response-as-a-file-attachment\">\n<h4>Telling the browser to treat the response as a file attachment<a class=\"heading-anchor\" href=\"#telling-the-browser-to-treat-the-response-as-a-file-attachment\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>To tell the browser to treat the response as a file attachment, set the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Disposition</span></code> headers. For example, this is how\nyou might return a Microsoft Excel spreadsheet:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span>\n<span class=\"gp\">... </span>    <span class=\"n\">my_data</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"n\">headers</span><span class=\"o\">=</span><span class=\"p\">{</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;Content-Type&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;application/vnd.ms-excel&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;Content-Disposition&quot;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;attachment; filename=&quot;foo.xls&quot;&#39;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"p\">},</span>\n<span class=\"gp\">... </span><span class=\"p\">)</span>\n</code></pre></div>\n<p>There’s nothing Django-specific about the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Disposition</span></code> header, but\nit’s easy to forget the syntax, so we’ve included it here.</p>\n</section>\n</section>\n<section id=\"id3\">\n<h3>Attributes<a class=\"heading-anchor\" href=\"#id3\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.content\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">content</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.content\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A bytestring representing the content, encoded from a string if necessary.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.cookies\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">cookies</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.cookies\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A <a class=\"reference external\" href=\"https://docs.python.org/3/library/http.cookies.html#http.cookies.SimpleCookie\" title=\"(in Python v3.14)\"><code class=\"xref py py-obj docutils literal notranslate\"><span class=\"pre\">http.cookies.SimpleCookie</span></code></a> object holding the cookies included\nin the response.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.headers\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">headers</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.headers\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A case insensitive, dict-like object that provides an interface to all\nHTTP headers on the response, except a <code class=\"docutils literal notranslate\"><span class=\"pre\">Set-Cookie</span></code> header. See\n<a class=\"reference internal\" href=\"#setting-header-fields\"><span class=\"std std-ref\">Setting header fields</span></a> and <a class=\"reference internal\" href=\"#django.http.HttpResponse.cookies\" title=\"django.http.HttpResponse.cookies\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">HttpResponse.cookies</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.charset\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">charset</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.charset\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A string denoting the charset in which the response will be encoded. If not\ngiven at <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code> instantiation time, it will be extracted from\n<code class=\"docutils literal notranslate\"><span class=\"pre\">content_type</span></code> and if that is unsuccessful, the\n<a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a> setting will be used.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.status_code\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">status_code</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.status_code\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <span class=\"target\" id=\"index-3\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc9110.html#section-15\"><strong>HTTP status code</strong></a> for the response.</p>\n<p>Unless <a class=\"reference internal\" href=\"#django.http.HttpResponse.reason_phrase\" title=\"django.http.HttpResponse.reason_phrase\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">reason_phrase</span></code></a> is explicitly set, modifying the value of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">status_code</span></code> outside the constructor will also modify the value of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">reason_phrase</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.reason_phrase\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">reason_phrase</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.reason_phrase\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The HTTP reason phrase for the response. It uses the <span class=\"target\" id=\"index-4\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc9110.html#section-15.1\"><strong>HTTP standard’s</strong></a> default reason phrases.</p>\n<p>Unless explicitly set, <code class=\"docutils literal notranslate\"><span class=\"pre\">reason_phrase</span></code> is determined by the value of\n<a class=\"reference internal\" href=\"#django.http.HttpResponse.status_code\" title=\"django.http.HttpResponse.status_code\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">status_code</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.streaming\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">streaming</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.streaming\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This is always <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>.</p>\n<p>This attribute exists so middleware can treat streaming responses\ndifferently from regular responses.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.closed\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">closed</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.closed\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> if the response has been closed.</p>\n</dd></dl>\n\n</section>\n<section id=\"id4\">\n<h3>Methods<a class=\"heading-anchor\" href=\"#id4\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.__init__\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">__init__</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">content</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">b''</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">content_type</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">status</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">200</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">reason</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">charset</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">headers</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.__init__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Instantiates an <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code> object with the given page content,\ncontent type, and headers.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">content</span></code> is most commonly an iterator, bytestring, <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#memoryview\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">memoryview</span></code></a>,\nor string. Other types will be converted to a bytestring by encoding their\nstring representation. Iterators should return strings or bytestrings and\nthose will be joined together to form the content of the response.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">content_type</span></code> is the MIME type optionally completed by a character set\nencoding and is used to fill the HTTP <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code> header. If not\nspecified, it is formed by <code class=\"docutils literal notranslate\"><span class=\"pre\">'text/html'</span></code> and the\n<a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a> settings, by default:\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;text/html;</span> <span class=\"pre\">charset=utf-8&quot;</span></code>.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">status</span></code> is the <span class=\"target\" id=\"index-5\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc9110.html#section-15\"><strong>HTTP status code</strong></a> for the\nresponse. You can use Python’s <a class=\"reference external\" href=\"https://docs.python.org/3/library/http.html#http.HTTPStatus\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">http.HTTPStatus</span></code></a> for meaningful\naliases, such as <code class=\"docutils literal notranslate\"><span class=\"pre\">HTTPStatus.NO_CONTENT</span></code>.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">reason</span></code> is the HTTP response phrase. If not provided, a default phrase\nwill be used.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">charset</span></code> is the charset in which the response will be encoded. If not\ngiven it will be extracted from <code class=\"docutils literal notranslate\"><span class=\"pre\">content_type</span></code>, and if that\nis unsuccessful, the <a class=\"reference internal\" href=\"/it/5.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a> setting will be used.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">headers</span></code> is a <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">dict</span></code></a> of HTTP headers for the response.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.__setitem__\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">__setitem__</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">header</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">value</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.__setitem__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Sets the given header name to the given value. Both <code class=\"docutils literal notranslate\"><span class=\"pre\">header</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">value</span></code> should be strings.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.__delitem__\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">__delitem__</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">header</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.__delitem__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Deletes the header with the given name. Fails silently if the header\ndoesn’t exist. Case-insensitive.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.__getitem__\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">__getitem__</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">header</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.__getitem__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the value for the given header name. Case-insensitive.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.get\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">get</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">header</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">alternate</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.get\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the value for the given header, or an <code class=\"docutils literal notranslate\"><span class=\"pre\">alternate</span></code> if the header\ndoesn’t exist.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.has_header\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">has_header</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">header</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.has_header\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code> based on a case-insensitive check for a\nheader with the given name.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.items\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">items</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.items\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Acts like <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#dict.items\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">dict.items()</span></code></a> for HTTP headers on the response.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.setdefault\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">setdefault</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">header</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">value</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.setdefault\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Sets a header unless it has already been set.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.set_cookie\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">set_cookie</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">value</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">''</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">max_age</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">expires</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">path</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'/'</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">domain</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">secure</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">httponly</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">samesite</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.set_cookie\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Sets a cookie. The parameters are the same as in the\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/http.cookies.html#http.cookies.Morsel\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Morsel</span></code></a> cookie object in the Python standard library.</p>\n<ul>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">max_age</span></code> should be a <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.timedelta\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">timedelta</span></code></a> object, an integer\nnumber of seconds, or <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> (default) if the cookie should last only\nas long as the client’s browser session. If <code class=\"docutils literal notranslate\"><span class=\"pre\">expires</span></code> is not specified,\nit will be calculated.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">expires</span></code> should either be a string in the format\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;Wdy,</span> <span class=\"pre\">DD-Mon-YY</span> <span class=\"pre\">HH:MM:SS</span> <span class=\"pre\">GMT&quot;</span></code> or a <code class=\"docutils literal notranslate\"><span class=\"pre\">datetime.datetime</span></code> object\nin UTC. If <code class=\"docutils literal notranslate\"><span class=\"pre\">expires</span></code> is a <code class=\"docutils literal notranslate\"><span class=\"pre\">datetime</span></code> object, the <code class=\"docutils literal notranslate\"><span class=\"pre\">max_age</span></code>\nwill be calculated.</p></li>\n<li><p>Use <code class=\"docutils literal notranslate\"><span class=\"pre\">domain</span></code> if you want to set a cross-domain cookie. For example,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">domain=&quot;example.com&quot;</span></code> will set a cookie that is readable by the\ndomains www.example.com, blog.example.com, etc. Otherwise, a cookie will\nonly be readable by the domain that set it.</p></li>\n<li><p>Use <code class=\"docutils literal notranslate\"><span class=\"pre\">secure=True</span></code> if you want the cookie to be only sent to the server\nwhen a request is made with the <code class=\"docutils literal notranslate\"><span class=\"pre\">https</span></code> scheme.</p></li>\n<li><p>Use <code class=\"docutils literal notranslate\"><span class=\"pre\">httponly=True</span></code> if you want to prevent client-side\nJavaScript from having access to the cookie.</p>\n<p><a class=\"reference external\" href=\"https://owasp.org/www-community/HttpOnly\">HttpOnly</a> is a flag included in a Set-Cookie HTTP response header. It’s\npart of the <span class=\"target\" id=\"index-6\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc6265.html#section-4.1.2.6\"><strong>RFC 6265</strong></a> standard for cookies\nand can be a useful way to mitigate the risk of a client-side script\naccessing the protected cookie data.</p>\n</li>\n<li><p>Use <code class=\"docutils literal notranslate\"><span class=\"pre\">samesite='Strict'</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">samesite='Lax'</span></code> to tell the browser not\nto send this cookie when performing a cross-origin request. <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value\">SameSite</a>\nisn’t supported by all browsers, so it’s not a replacement for Django’s\nCSRF protection, but rather a defense in depth measure.</p>\n<p>Use <code class=\"docutils literal notranslate\"><span class=\"pre\">samesite='None'</span></code> (string) to explicitly state that this cookie is\nsent with all same-site and cross-site requests.</p>\n</li>\n</ul>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Avvertimento</p>\n<p><span class=\"target\" id=\"index-7\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc6265.html#section-6.1\"><strong>RFC 6265</strong></a> states that user agents should\nsupport cookies of at least 4096 bytes. For many browsers this is also\nthe maximum size. Django will not raise an exception if there’s an\nattempt to store a cookie of more than 4096 bytes, but many browsers\nwill not set the cookie correctly.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.set_signed_cookie\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">set_signed_cookie</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">value</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">salt</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">''</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">max_age</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">expires</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">path</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'/'</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">domain</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">secure</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">httponly</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">samesite</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.set_signed_cookie\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Like <a class=\"reference internal\" href=\"#django.http.HttpResponse.set_cookie\" title=\"django.http.HttpResponse.set_cookie\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">set_cookie()</span></code></a>, but\n<a class=\"reference internal\" href=\"/it/5.0/topics/signing/\"><span class=\"doc\">cryptographic signing</span></a> the cookie before setting\nit. Use in conjunction with <a class=\"reference internal\" href=\"#django.http.HttpRequest.get_signed_cookie\" title=\"django.http.HttpRequest.get_signed_cookie\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">HttpRequest.get_signed_cookie()</span></code></a>.\nYou can use the optional <code class=\"docutils literal notranslate\"><span class=\"pre\">salt</span></code> argument for added key strength, but\nyou will need to remember to pass it to the corresponding\n<a class=\"reference internal\" href=\"#django.http.HttpRequest.get_signed_cookie\" title=\"django.http.HttpRequest.get_signed_cookie\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">HttpRequest.get_signed_cookie()</span></code></a> call.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.delete_cookie\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">delete_cookie</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">key</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">path</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'/'</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">domain</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">samesite</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.delete_cookie\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Deletes the cookie with the given key. Fails silently if the key doesn’t\nexist.</p>\n<p>Due to the way cookies work, <code class=\"docutils literal notranslate\"><span class=\"pre\">path</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">domain</span></code> should be the same\nvalues you used in <code class=\"docutils literal notranslate\"><span class=\"pre\">set_cookie()</span></code> – otherwise the cookie may not be\ndeleted.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.close\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">close</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.close\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This method is called at the end of the request directly by the WSGI\nserver.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.write\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">write</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">content</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.write\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This method makes an <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> instance a file-like object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.flush\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">flush</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.flush\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This method makes an <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> instance a file-like object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.tell\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">tell</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.tell\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This method makes an <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> instance a file-like object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.getvalue\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">getvalue</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.getvalue\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the value of <a class=\"reference internal\" href=\"#django.http.HttpResponse.content\" title=\"django.http.HttpResponse.content\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">HttpResponse.content</span></code></a>. This method makes\nan <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> instance a stream-like object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.readable\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">readable</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.readable\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Always <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>. This method makes an <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> instance a\nstream-like object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.seekable\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">seekable</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.seekable\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Always <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>. This method makes an <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> instance a\nstream-like object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.writable\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">writable</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.writable\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Always <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>. This method makes an <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> instance a\nstream-like object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponse.writelines\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">HttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">writelines</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">lines</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.HttpResponse.writelines\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Writes a list of lines to the response. Line separators are not added. This\nmethod makes an <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> instance a stream-like object.</p>\n</dd></dl>\n\n</section>\n<section id=\"httpresponse-subclasses\">\n<span id=\"ref-httpresponse-subclasses\"></span><h3><code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code> subclasses<a class=\"heading-anchor\" href=\"#httpresponse-subclasses\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django includes a number of <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code> subclasses that handle different\ntypes of HTTP responses. Like <code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code>, these subclasses live in\n<a class=\"reference internal\" href=\"#module-django.http\" title=\"django.http: Classes dealing with HTTP requests and responses.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.http</span></code></a>.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponseRedirect\">\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\">HttpResponseRedirect</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponseRedirect\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The first argument to the constructor is required – the path to redirect\nto. This can be a fully qualified URL\n(e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">'https://www.yahoo.com/search/'</span></code>), an absolute path with no domain\n(e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">'/search/'</span></code>), or even a relative path (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">'search/'</span></code>). In that\nlast case, the client browser will reconstruct the full URL itself\naccording to the current path. See <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> for other optional\nconstructor arguments. Note that this returns an HTTP status code 302.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponseRedirect.url\">\n<span class=\"sig-name descname\"><span class=\"pre\">url</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponseRedirect.url\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This read-only attribute represents the URL the response will redirect\nto (equivalent to the <code class=\"docutils literal notranslate\"><span class=\"pre\">Location</span></code> response header).</p>\n</dd></dl>\n\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponsePermanentRedirect\">\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\">HttpResponsePermanentRedirect</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponsePermanentRedirect\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Like <a class=\"reference internal\" href=\"#django.http.HttpResponseRedirect\" title=\"django.http.HttpResponseRedirect\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponseRedirect</span></code></a>, but it returns a permanent redirect\n(HTTP status code 301) instead of a «found» redirect (status code 302).</p>\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponseNotModified\">\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\">HttpResponseNotModified</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponseNotModified\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The constructor doesn’t take any arguments and no content should be added\nto this response. Use this to designate that a page hasn’t been modified\nsince the user’s last request (status code 304).</p>\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponseBadRequest\">\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\">HttpResponseBadRequest</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponseBadRequest\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Acts just like <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> but uses a 400 status code.</p>\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponseNotFound\">\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\">HttpResponseNotFound</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponseNotFound\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Acts just like <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> but uses a 404 status code.</p>\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponseForbidden\">\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\">HttpResponseForbidden</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponseForbidden\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Acts just like <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> but uses a 403 status code.</p>\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponseNotAllowed\">\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\">HttpResponseNotAllowed</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponseNotAllowed\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Like <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a>, but uses a 405 status code. The first argument\nto the constructor is required: a list of permitted methods (e.g.\n<code class=\"docutils literal notranslate\"><span class=\"pre\">['GET',</span> <span class=\"pre\">'POST']</span></code>).</p>\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponseGone\">\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\">HttpResponseGone</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponseGone\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Acts just like <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> but uses a 410 status code.</p>\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponseServerError\">\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\">HttpResponseServerError</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponseServerError\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Acts just like <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> but uses a 500 status code.</p>\n</dd></dl>\n\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>If a custom subclass of <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> implements a <code class=\"docutils literal notranslate\"><span class=\"pre\">render</span></code>\nmethod, Django will treat it as emulating a\n<a class=\"reference internal\" href=\"/it/5.0/ref/template-response/#django.template.response.SimpleTemplateResponse\" title=\"django.template.response.SimpleTemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SimpleTemplateResponse</span></code></a>, and the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">render</span></code> method must itself return a valid response object.</p>\n</aside>\n<section id=\"custom-response-classes\">\n<h4>Custom response classes<a class=\"heading-anchor\" href=\"#custom-response-classes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>If you find yourself needing a response class that Django doesn’t provide, you\ncan create it with the help of <a class=\"reference external\" href=\"https://docs.python.org/3/library/http.html#http.HTTPStatus\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">http.HTTPStatus</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\">http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HTTPStatus</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HttpResponse</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">HttpResponseNoContent</span><span class=\"p\">(</span><span class=\"n\">HttpResponse</span><span class=\"p\">):</span>\n    <span class=\"n\">status_code</span> <span class=\"o\">=</span> <span class=\"n\">HTTPStatus</span><span class=\"o\">.</span><span class=\"n\">NO_CONTENT</span>\n</code></pre></div>\n</section>\n</section>\n</section>\n<section id=\"jsonresponse-objects\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">JsonResponse</span></code> objects<a class=\"heading-anchor\" href=\"#jsonresponse-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.JsonResponse\">\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\">JsonResponse</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">data</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">encoder</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">DjangoJSONEncoder</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">safe</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">True</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">json_dumps_params</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">**</span></span><span class=\"n\"><span class=\"pre\">kwargs</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.JsonResponse\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>An <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> subclass that helps to create a JSON-encoded\nresponse. It inherits most behavior from its superclass with a couple\ndifferences:</p>\n<p>Its default <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code> header is set to <em class=\"mimetype\">application/json</em>.</p>\n<p>The first parameter, <code class=\"docutils literal notranslate\"><span class=\"pre\">data</span></code>, should be a <code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code> instance. If the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">safe</span></code> parameter is set to <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code> (see below) it can be any\nJSON-serializable object.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">encoder</span></code>, which defaults to\n<a class=\"reference internal\" href=\"/it/5.0/topics/serialization/#django.core.serializers.json.DjangoJSONEncoder\" title=\"django.core.serializers.json.DjangoJSONEncoder\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.core.serializers.json.DjangoJSONEncoder</span></code></a>, will be used to\nserialize the data. See <a class=\"reference internal\" href=\"/it/5.0/topics/serialization/#serialization-formats-json\"><span class=\"std std-ref\">JSON serialization</span></a> for more details about this serializer.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">safe</span></code> boolean parameter defaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>. If it’s set to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>, any object can be passed for serialization (otherwise only\n<code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code> instances are allowed). If <code class=\"docutils literal notranslate\"><span class=\"pre\">safe</span></code> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> and a non-<code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code>\nobject is passed as the first argument, a <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#TypeError\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">TypeError</span></code></a> will be raised.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">json_dumps_params</span></code> parameter is a dictionary of keyword arguments\nto pass to the <code class=\"docutils literal notranslate\"><span class=\"pre\">json.dumps()</span></code> call used to generate the response.</p>\n</dd></dl>\n\n<section id=\"id5\">\n<h3>Usage<a class=\"heading-anchor\" href=\"#id5\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Typical usage could look like:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">JsonResponse</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">JsonResponse</span><span class=\"p\">({</span><span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;bar&quot;</span><span class=\"p\">})</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">content</span>\n<span class=\"go\">b&#39;{&quot;foo&quot;: &quot;bar&quot;}&#39;</span>\n</code></pre></div>\n<section id=\"serializing-non-dictionary-objects\">\n<h4>Serializing non-dictionary objects<a class=\"heading-anchor\" href=\"#serializing-non-dictionary-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>In order to serialize objects other than <code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code> you must set the <code class=\"docutils literal notranslate\"><span class=\"pre\">safe</span></code>\nparameter to <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">JsonResponse</span><span class=\"p\">([</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">],</span> <span class=\"n\">safe</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Without passing <code class=\"docutils literal notranslate\"><span class=\"pre\">safe=False</span></code>, a <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#TypeError\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">TypeError</span></code></a> will be raised.</p>\n<p>Note that an API based on <code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code> objects is more extensible, flexible, and\nmakes it easier to maintain forwards compatibility. Therefore, you should avoid\nusing non-dict objects in JSON-encoded response.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Avvertimento</p>\n<p>Before the <a class=\"reference external\" href=\"https://262.ecma-international.org/5.1/#sec-11.1.4\">5th edition of ECMAScript</a> it was possible to\npoison the JavaScript <code class=\"docutils literal notranslate\"><span class=\"pre\">Array</span></code> constructor. For this reason, Django does\nnot allow passing non-dict objects to the\n<a class=\"reference internal\" href=\"#django.http.JsonResponse\" title=\"django.http.JsonResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">JsonResponse</span></code></a> constructor by default.  However, most\nmodern browsers implement ECMAScript 5 which removes this attack vector.\nTherefore it is possible to disable this security precaution.</p>\n</aside>\n</section>\n<section id=\"changing-the-default-json-encoder\">\n<h4>Changing the default JSON encoder<a class=\"heading-anchor\" href=\"#changing-the-default-json-encoder\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>If you need to use a different JSON encoder class you can pass the <code class=\"docutils literal notranslate\"><span class=\"pre\">encoder</span></code>\nparameter to the constructor method:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">JsonResponse</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">,</span> <span class=\"n\">encoder</span><span class=\"o\">=</span><span class=\"n\">MyJSONEncoder</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n</section>\n</section>\n<section id=\"streaminghttpresponse-objects\">\n<span id=\"httpresponse-streaming\"></span><h2><code class=\"docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code> objects<a class=\"heading-anchor\" href=\"#streaminghttpresponse-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.StreamingHttpResponse\">\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\">StreamingHttpResponse</span></span><a class=\"heading-anchor\" href=\"#django.http.StreamingHttpResponse\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The <a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a> class is used to stream a response from\nDjango to the browser.</p>\n<aside class=\"admonition-advanced-usage admonition\">\n<p class=\"admonition-title\">Advanced usage</p>\n<p><a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a> is somewhat advanced, in that it is\nimportant to know whether you’ll be serving your application synchronously\nunder WSGI or asynchronously under ASGI, and adjust your usage\nappropriately.</p>\n<p>Please read these notes with care.</p>\n</aside>\n<p>An example usage of <a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a> under WSGI is streaming\ncontent when generating the response would take too long or uses too much\nmemory. For instance, it’s useful for <a class=\"reference internal\" href=\"/it/5.0/howto/outputting-csv/#streaming-csv-files\"><span class=\"std std-ref\">generating large CSV files</span></a>.</p>\n<p>There are performance considerations when doing this, though. Django, under\nWSGI, is designed for short-lived requests. Streaming responses will tie a\nworker process for the entire duration of the response. This may result in poor\nperformance.</p>\n<p>Generally speaking, you would perform expensive tasks outside of the\nrequest-response cycle, rather than resorting to a streamed response.</p>\n<p>When serving under ASGI, however, a <a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a> need not\nstop other requests from being served whilst waiting for I/O. This opens up\nthe possibility of long-lived requests for streaming content and implementing\npatterns such as long-polling, and server-sent events.</p>\n<p>Even under ASGI note, <a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a> should only be used in\nsituations where it is absolutely required that the whole content isn’t\niterated before transferring the data to the client. Because the content can’t\nbe accessed, many middleware can’t function normally. For example the <code class=\"docutils literal notranslate\"><span class=\"pre\">ETag</span></code>\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code> headers can’t be generated for streaming responses.</p>\n<p>The <a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a> is not a subclass of <a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a>,\nbecause it features a slightly different API. However, it is almost identical,\nwith the following notable differences:</p>\n<ul>\n<li><p>It should be given an iterator that yields bytestrings, <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#memoryview\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">memoryview</span></code></a>,\nor strings as content. When serving under WSGI, this should be a sync\niterator. When serving under ASGI, then it should be an async iterator.</p></li>\n<li><p>You cannot access its content, except by iterating the response object\nitself. This should only occur when the response is returned to the client:\nyou should not iterate the response yourself.</p>\n<p>Under WSGI the response will be iterated synchronously. Under ASGI the\nresponse will be iterated asynchronously. (This is why the iterator type must\nmatch the protocol you’re using.)</p>\n<p>To avoid a crash, an incorrect iterator type will be mapped to the correct\ntype during iteration, and a warning will be raised, but in order to do this\nthe iterator must be fully-consumed, which defeats the purpose of using a\n<a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a> at all.</p>\n</li>\n<li><p>It has no <code class=\"docutils literal notranslate\"><span class=\"pre\">content</span></code> attribute. Instead, it has a\n<a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse.streaming_content\" title=\"django.http.StreamingHttpResponse.streaming_content\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">streaming_content</span></code></a> attribute. This can be used\nin middleware to wrap the response iterable, but should not be consumed.</p></li>\n<li><p>You cannot use the file-like object <code class=\"docutils literal notranslate\"><span class=\"pre\">tell()</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">write()</span></code> methods.\nDoing so will raise an exception.</p></li>\n</ul>\n<p>The <a class=\"reference internal\" href=\"#django.http.HttpResponseBase\" title=\"django.http.HttpResponseBase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponseBase</span></code></a> base class is common between\n<a class=\"reference internal\" href=\"#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> and <a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a>.</p>\n<aside class=\"version-note version-changed\" data-version=\"4.2\">\n<p class=\"version-note-title\">Changed in Django 4.2</p><p>Support for asynchronous iteration was added.</p>\n</aside>\n<section id=\"id6\">\n<h3>Attributes<a class=\"heading-anchor\" href=\"#id6\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.StreamingHttpResponse.streaming_content\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">StreamingHttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">streaming_content</span></span><a class=\"heading-anchor\" href=\"#django.http.StreamingHttpResponse.streaming_content\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>An iterator of the response content, bytestring encoded according to\n<a class=\"reference internal\" href=\"#django.http.HttpResponse.charset\" title=\"django.http.HttpResponse.charset\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">HttpResponse.charset</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.StreamingHttpResponse.status_code\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">StreamingHttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">status_code</span></span><a class=\"heading-anchor\" href=\"#django.http.StreamingHttpResponse.status_code\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <span class=\"target\" id=\"index-8\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc9110.html#section-15\"><strong>HTTP status code</strong></a> for the response.</p>\n<p>Unless <a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse.reason_phrase\" title=\"django.http.StreamingHttpResponse.reason_phrase\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">reason_phrase</span></code></a> is explicitly set, modifying the value of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">status_code</span></code> outside the constructor will also modify the value of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">reason_phrase</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.StreamingHttpResponse.reason_phrase\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">StreamingHttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">reason_phrase</span></span><a class=\"heading-anchor\" href=\"#django.http.StreamingHttpResponse.reason_phrase\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The HTTP reason phrase for the response. It uses the <span class=\"target\" id=\"index-9\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc9110.html#section-15.1\"><strong>HTTP standard’s</strong></a> default reason phrases.</p>\n<p>Unless explicitly set, <code class=\"docutils literal notranslate\"><span class=\"pre\">reason_phrase</span></code> is determined by the value of\n<a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse.status_code\" title=\"django.http.StreamingHttpResponse.status_code\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">status_code</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.StreamingHttpResponse.streaming\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">StreamingHttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">streaming</span></span><a class=\"heading-anchor\" href=\"#django.http.StreamingHttpResponse.streaming\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This is always <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.http.StreamingHttpResponse.is_async\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">StreamingHttpResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">is_async</span></span><a class=\"heading-anchor\" href=\"#django.http.StreamingHttpResponse.is_async\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"4.2\">\n<p class=\"version-note-title\">New in Django 4.2</p></aside>\n<p>Boolean indicating whether <a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse.streaming_content\" title=\"django.http.StreamingHttpResponse.streaming_content\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse.streaming_content</span></code></a>\nis an asynchronous iterator or not.</p>\n<p>This is useful for middleware needing to wrap\n<a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse.streaming_content\" title=\"django.http.StreamingHttpResponse.streaming_content\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse.streaming_content</span></code></a>.</p>\n</dd></dl>\n\n</section>\n<section id=\"handling-disconnects\">\n<span id=\"request-response-streaming-disconnect\"></span><h3>Handling disconnects<a class=\"heading-anchor\" href=\"#handling-disconnects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"version-note version-added\" data-version=\"5.0\">\n<p class=\"version-note-title\">New in Django 5.0</p></aside>\n<p>If the client disconnects during a streaming response, Django will cancel the\ncoroutine that is handling the response. If you want to clean up resources\nmanually, you can do so by catching the <code class=\"docutils literal notranslate\"><span class=\"pre\">asyncio.CancelledError</span></code>:</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=\"k\">async</span> <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">streaming_response</span><span class=\"p\">():</span>\n    <span class=\"k\">try</span><span class=\"p\">:</span>\n        <span class=\"c1\"># Do some work here</span>\n        <span class=\"k\">async</span> <span class=\"k\">for</span> <span class=\"n\">chunk</span> <span class=\"ow\">in</span> <span class=\"n\">my_streaming_iterator</span><span class=\"p\">():</span>\n            <span class=\"k\">yield</span> <span class=\"n\">chunk</span>\n    <span class=\"k\">except</span> <span class=\"n\">asyncio</span><span class=\"o\">.</span><span class=\"n\">CancelledError</span><span class=\"p\">:</span>\n        <span class=\"c1\"># Handle disconnect</span>\n        <span class=\"o\">...</span>\n        <span class=\"k\">raise</span>\n\n\n<span class=\"k\">async</span> <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">my_streaming_view</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"n\">StreamingHttpResponse</span><span class=\"p\">(</span><span class=\"n\">streaming_response</span><span class=\"p\">())</span>\n</code></pre></div>\n<p>This example only shows how to handle client disconnection while the response\nis streaming. If you perform long-running operations in your view before\nreturning the <code class=\"docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code> object, then you may also want to\n<a class=\"reference internal\" href=\"/it/5.0/topics/async/#async-handling-disconnect\"><span class=\"std std-ref\">handle disconnections in the view</span></a> itself.</p>\n</section>\n</section>\n<section id=\"fileresponse-objects\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">FileResponse</span></code> objects<a class=\"heading-anchor\" href=\"#fileresponse-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.FileResponse\">\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\">FileResponse</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">open_file</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">as_attachment</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">filename</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">''</span></span></em>, <em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">**</span></span><span class=\"n\"><span class=\"pre\">kwargs</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.FileResponse\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><a class=\"reference internal\" href=\"#django.http.FileResponse\" title=\"django.http.FileResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FileResponse</span></code></a> is a subclass of <a class=\"reference internal\" href=\"#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a>\noptimized for binary files. It uses <span class=\"target\" id=\"index-10\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-3333/#optional-platform-specific-file-handling\"><strong>wsgi.file_wrapper</strong></a> if provided by the wsgi\nserver, otherwise it streams the file out in small chunks.</p>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">as_attachment=True</span></code>, the <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Disposition</span></code> header is set to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">attachment</span></code>, which asks the browser to offer the file to the user as a\ndownload. Otherwise, a <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Disposition</span></code> header with a value of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">inline</span></code> (the browser default) will be set only if a filename is\navailable.</p>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">open_file</span></code> doesn’t have a name or if the name of <code class=\"docutils literal notranslate\"><span class=\"pre\">open_file</span></code> isn’t\nappropriate, provide a custom file name using the <code class=\"docutils literal notranslate\"><span class=\"pre\">filename</span></code>  parameter.\nNote that if you pass a file-like object like <code class=\"docutils literal notranslate\"><span class=\"pre\">io.BytesIO</span></code>, it’s your\ntask to <code class=\"docutils literal notranslate\"><span class=\"pre\">seek()</span></code> it before passing it to <code class=\"docutils literal notranslate\"><span class=\"pre\">FileResponse</span></code>.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code> header is automatically set when it can be guessed\nfrom the content of <code class=\"docutils literal notranslate\"><span class=\"pre\">open_file</span></code>.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code> header is automatically set when it can be guessed\nfrom the <code class=\"docutils literal notranslate\"><span class=\"pre\">filename</span></code>, or the name of <code class=\"docutils literal notranslate\"><span class=\"pre\">open_file</span></code>.</p>\n</dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">FileResponse</span></code> accepts any file-like object with binary content, for example\na file open in binary mode like so:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">FileResponse</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"n\">FileResponse</span><span class=\"p\">(</span><span class=\"nb\">open</span><span class=\"p\">(</span><span class=\"s2\">&quot;myfile.png&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;rb&quot;</span><span class=\"p\">))</span>\n</code></pre></div>\n<p>The file will be closed automatically, so don’t open it with a context manager.</p>\n<aside class=\"admonition-use-under-asgi admonition\">\n<p class=\"admonition-title\">Use under ASGI</p>\n<p>Python’s file API is synchronous. This means that the file must be fully\nconsumed in order to be served under ASGI.</p>\n<p>In order to stream a file asynchronously you need to use a third-party\npackage that provides an asynchronous file API, such as <a class=\"reference external\" href=\"https://github.com/Tinche/aiofiles\">aiofiles</a>.</p>\n</aside>\n<section id=\"id7\">\n<h3>Methods<a class=\"heading-anchor\" href=\"#id7\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.http.FileResponse.set_headers\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">FileResponse.</span></span><span class=\"sig-name descname\"><span class=\"pre\">set_headers</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">open_file</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.http.FileResponse.set_headers\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This method is automatically called during the response initialization and\nset various headers (<code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Length</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Type</span></code>, and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Content-Disposition</span></code>) depending on <code class=\"docutils literal notranslate\"><span class=\"pre\">open_file</span></code>.</p>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"httpresponsebase-class\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">HttpResponseBase</span></code> class<a class=\"heading-anchor\" href=\"#httpresponsebase-class\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.http.HttpResponseBase\">\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\">HttpResponseBase</span></span><a class=\"heading-anchor\" href=\"#django.http.HttpResponseBase\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The <a class=\"reference internal\" href=\"#django.http.HttpResponseBase\" title=\"django.http.HttpResponseBase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponseBase</span></code></a> class is common to all Django responses.\nIt should not be used to create responses directly, but it can be\nuseful for type-checking.</p>\n</section>","rootId":"module-django.http","toc":[{"title":"Quick overview","anchor":"quick-overview","children":[]},{"title":"HttpRequest objects","anchor":"httprequest-objects","children":[{"title":"Attributes","anchor":"attributes","children":[]},{"title":"Attributes set by application code","anchor":"attributes-set-by-application-code","children":[]},{"title":"Attributes set by middleware","anchor":"attributes-set-by-middleware","children":[]},{"title":"Methods","anchor":"methods","children":[]}]},{"title":"QueryDict objects","anchor":"querydict-objects","children":[{"title":"Methods","anchor":"id1","children":[]}]},{"title":"HttpResponse objects","anchor":"httpresponse-objects","children":[{"title":"Usage","anchor":"usage","children":[{"title":"Passing strings","anchor":"passing-strings","children":[]},{"title":"Passing iterators","anchor":"passing-iterators","children":[]},{"title":"Setting header fields","anchor":"setting-header-fields","children":[]},{"title":"Telling the browser to treat the response as a file attachment","anchor":"telling-the-browser-to-treat-the-response-as-a-file-attachment","children":[]}]},{"title":"Attributes","anchor":"id3","children":[]},{"title":"Methods","anchor":"id4","children":[]},{"title":"HttpResponse subclasses","anchor":"httpresponse-subclasses","children":[{"title":"Custom response classes","anchor":"custom-response-classes","children":[]}]}]},{"title":"JsonResponse objects","anchor":"jsonresponse-objects","children":[{"title":"Usage","anchor":"id5","children":[{"title":"Serializing non-dictionary objects","anchor":"serializing-non-dictionary-objects","children":[]},{"title":"Changing the default JSON encoder","anchor":"changing-the-default-json-encoder","children":[]}]}]},{"title":"StreamingHttpResponse objects","anchor":"streaminghttpresponse-objects","children":[{"title":"Attributes","anchor":"id6","children":[]},{"title":"Handling disconnects","anchor":"handling-disconnects","children":[]}]},{"title":"FileResponse objects","anchor":"fileresponse-objects","children":[{"title":"Methods","anchor":"id7","children":[]}]},{"title":"HttpResponseBase class","anchor":"httpresponsebase-class","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/it/5.0/ref/"}],"prev":{"docname":"ref/paginator","title":"Paginator","url":"/it/5.0/ref/paginator/"},"next":{"docname":"ref/schema-editor","title":"SchemaEditor","url":"/it/5.0/ref/schema-editor/"},"formats":{"html":"/it/5.0/ref/request-response/","markdown":"/it/5.0/ref/request-response.md","json":"/it/5.0/ref/request-response.json"},"source":"https://github.com/django/django/blob/stable/5.0.x/docs/ref/request-response.txt","official":"https://docs.djangoproject.com/it/5.0/ref/request-response/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}