{"title":"Middleware","version":"1.9","locale":"es","docname":"topics/http/middleware","url":"/es/1.9/topics/http/middleware/","canonical":"https://djangodocs.dev/es/1.9/topics/http/middleware/","summary":"Middleware is a framework of hooks into Django’s request/response processing. It’s a light, low-level «plugin» system for globally altering Django’s input or…","html":"<h1>Middleware<a class=\"heading-anchor\" href=\"#middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Middleware is a framework of hooks into Django’s request/response processing.\nIt’s a light, low-level «plugin» system for globally altering Django’s input\nor output.</p>\n<p>Each middleware component is responsible for doing some specific function. For\nexample, Django includes a middleware component,\n<a class=\"reference internal\" href=\"/es/1.9/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>, that\nassociates users with requests using sessions.</p>\n<p>This document explains how middleware works, how you activate middleware, and\nhow to write your own middleware. Django ships with some built-in middleware\nyou can use right out of the box. They’re documented in the <a class=\"reference internal\" href=\"/es/1.9/ref/middleware/\"><span class=\"doc\">built-in\nmiddleware reference</span></a>.</p>\n<section id=\"activating-middleware\">\n<h2>Activating middleware<a class=\"heading-anchor\" href=\"#activating-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To activate a middleware component, add it to the\n<a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-MIDDLEWARE_CLASSES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code></a> list in your Django settings.</p>\n<p>In <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-MIDDLEWARE_CLASSES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code></a>, each middleware component is represented by\na string: the full Python path to the middleware’s class name. For example,\nhere’s the default value created by <a class=\"reference internal\" href=\"/es/1.9/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">django-admin</span> <span class=\"pre\">startproject</span></code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">MIDDLEWARE_CLASSES</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s1\">&#39;django.middleware.security.SecurityMiddleware&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.sessions.middleware.SessionMiddleware&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.middleware.common.CommonMiddleware&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.middleware.csrf.CsrfViewMiddleware&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.auth.middleware.AuthenticationMiddleware&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.auth.middleware.SessionAuthenticationMiddleware&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.messages.middleware.MessageMiddleware&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.middleware.clickjacking.XFrameOptionsMiddleware&#39;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>A Django installation doesn’t require any middleware —\n<a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-MIDDLEWARE_CLASSES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code></a> can be empty, if you’d like — but it’s strongly\nsuggested that you at least use\n<a class=\"reference internal\" href=\"/es/1.9/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>.</p>\n<p>The order in <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-MIDDLEWARE_CLASSES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code></a> matters because a middleware can\ndepend on other middleware. For instance,\n<a class=\"reference internal\" href=\"/es/1.9/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> stores the\nauthenticated user in the session; therefore, it must run after\n<a class=\"reference internal\" href=\"/es/1.9/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>. See\n<a class=\"reference internal\" href=\"/es/1.9/ref/middleware/#middleware-ordering\"><span class=\"std std-ref\">Middleware ordering</span></a> for some common hints about ordering of Django\nmiddleware classes.</p>\n</section>\n<section id=\"hooks-and-application-order\">\n<h2>Hooks and application order<a class=\"heading-anchor\" href=\"#hooks-and-application-order\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>During the request phase, before calling the view, Django applies middleware\nin the order it’s defined in <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-MIDDLEWARE_CLASSES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code></a>, top-down. Two\nhooks are available:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"#process_request\" title=\"process_request\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">process_request()</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#process_view\" title=\"process_view\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">process_view()</span></code></a></p></li>\n</ul>\n<p>During the response phase, after calling the view, middleware are applied in\nreverse order, from the bottom up. Three hooks are available:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"#process_exception\" title=\"process_exception\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">process_exception()</span></code></a> (only if the view raised an exception)</p></li>\n<li><p><a class=\"reference internal\" href=\"#process_template_response\" title=\"process_template_response\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">process_template_response()</span></code></a> (only for template responses)</p></li>\n<li><p><a class=\"reference internal\" href=\"#process_response\" title=\"process_response\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">process_response()</span></code></a></p></li>\n</ul>\n<a class=\"reference internal image-reference\" href=\"../../../../../../_images/middleware.svg\"><img alt=\"middleware application order\" src=\"/es/1.9/_images/middleware.svg\" style=\"width: 481px; height: 409px;\" />\n</a>\n<p>If you prefer, you can also think of it like an onion: each middleware class\nis a «layer» that wraps the view.</p>\n<p>The behavior of each hook is described below.</p>\n</section>\n<section id=\"writing-your-own-middleware\">\n<h2>Writing your own middleware<a class=\"heading-anchor\" href=\"#writing-your-own-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Writing your own middleware is easy. Each middleware component is a single\nPython class that defines one or more of the following methods:</p>\n<section id=\"process-request\">\n<span id=\"request-middleware\"></span><h3><code class=\"docutils literal notranslate\"><span class=\"pre\">process_request()</span></code><a class=\"heading-anchor\" href=\"#process-request\"><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=\"process_request\">\n<span class=\"sig-name descname\"><span class=\"pre\">process_request</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">request</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#process_request\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">request</span></code> is an <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> object.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">process_request()</span></code> is called on each request, before Django decides which\nview to execute.</p>\n<p>It should return either <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> or an <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a>\nobject. If it returns <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code>, Django will continue processing this request,\nexecuting any other <code class=\"docutils literal notranslate\"><span class=\"pre\">process_request()</span></code> middleware, then, <code class=\"docutils literal notranslate\"><span class=\"pre\">process_view()</span></code>\nmiddleware, and finally, the appropriate view. If it returns an\n<a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> object, Django won’t bother calling any\nother request, view or exception middleware, or the appropriate view; it’ll\napply response middleware to that <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a>, and\nreturn the result.</p>\n</section>\n<section id=\"process-view\">\n<span id=\"view-middleware\"></span><h3><code class=\"docutils literal notranslate\"><span class=\"pre\">process_view()</span></code><a class=\"heading-anchor\" href=\"#process-view\"><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=\"process_view\">\n<span class=\"sig-name descname\"><span class=\"pre\">process_view</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">request</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">view_func</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">view_args</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">view_kwargs</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#process_view\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">request</span></code> is an <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> object. <code class=\"docutils literal notranslate\"><span class=\"pre\">view_func</span></code> is\nthe Python function that Django is about to use. (It’s the actual function\nobject, not the name of the function as a string.) <code class=\"docutils literal notranslate\"><span class=\"pre\">view_args</span></code> is a list of\npositional arguments that will be passed to the view, and <code class=\"docutils literal notranslate\"><span class=\"pre\">view_kwargs</span></code> is a\ndictionary of keyword arguments that will be passed to the view. Neither\n<code class=\"docutils literal notranslate\"><span class=\"pre\">view_args</span></code> nor <code class=\"docutils literal notranslate\"><span class=\"pre\">view_kwargs</span></code> include the first view argument\n(<code class=\"docutils literal notranslate\"><span class=\"pre\">request</span></code>).</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">process_view()</span></code> is called just before Django calls the view.</p>\n<p>It should return either <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> or an <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a>\nobject. If it returns <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code>, Django will continue processing this request,\nexecuting any other <code class=\"docutils literal notranslate\"><span class=\"pre\">process_view()</span></code> middleware and, then, the appropriate\nview. If it returns an <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> object, Django won’t\nbother calling any other view or exception middleware, or the appropriate\nview; it’ll apply response middleware to that\n<a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a>, and return the result.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>Accessing <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpRequest.POST\" title=\"django.http.HttpRequest.POST\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">request.POST</span></code></a> inside\nmiddleware from <code class=\"docutils literal notranslate\"><span class=\"pre\">process_request</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">process_view</span></code> will prevent any\nview running after the middleware from being able to <a class=\"reference internal\" href=\"/es/1.9/topics/http/file-uploads/#modifying-upload-handlers-on-the-fly\"><span class=\"std std-ref\">modify the\nupload handlers for the request</span></a>,\nand should normally be avoided.</p>\n<p>The <a class=\"reference internal\" href=\"/es/1.9/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> class can be\nconsidered an exception, as it provides the\n<a class=\"reference internal\" href=\"/es/1.9/ref/csrf/#django.views.decorators.csrf.csrf_exempt\" title=\"django.views.decorators.csrf.csrf_exempt\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">csrf_exempt()</span></code></a> and\n<a class=\"reference internal\" href=\"/es/1.9/ref/csrf/#django.views.decorators.csrf.csrf_protect\" title=\"django.views.decorators.csrf.csrf_protect\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">csrf_protect()</span></code></a> decorators which allow\nviews to explicitly control at what point the CSRF validation should occur.</p>\n</aside>\n</section>\n<section id=\"process-template-response\">\n<span id=\"template-response-middleware\"></span><h3><code class=\"docutils literal notranslate\"><span class=\"pre\">process_template_response()</span></code><a class=\"heading-anchor\" href=\"#process-template-response\"><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=\"process_template_response\">\n<span class=\"sig-name descname\"><span class=\"pre\">process_template_response</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">request</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">response</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#process_template_response\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">request</span></code> is an <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> object. <code class=\"docutils literal notranslate\"><span class=\"pre\">response</span></code> is\nthe <a class=\"reference internal\" href=\"/es/1.9/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TemplateResponse</span></code></a> object (or equivalent)\nreturned by a Django view or by a middleware.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">process_template_response()</span></code> is called just after the view has finished\nexecuting, if the response instance has a <code class=\"docutils literal notranslate\"><span class=\"pre\">render()</span></code> method, indicating that\nit is a <a class=\"reference internal\" href=\"/es/1.9/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TemplateResponse</span></code></a> or equivalent.</p>\n<p>It must return a response object that implements a <code class=\"docutils literal notranslate\"><span class=\"pre\">render</span></code> method. It could\nalter the given <code class=\"docutils literal notranslate\"><span class=\"pre\">response</span></code> by changing <code class=\"docutils literal notranslate\"><span class=\"pre\">response.template_name</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">response.context_data</span></code>, or it could create and return a brand-new\n<a class=\"reference internal\" href=\"/es/1.9/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TemplateResponse</span></code></a> or equivalent.</p>\n<p>You don’t need to explicitly render responses – responses will be\nautomatically rendered once all template response middleware has been\ncalled.</p>\n<p>Middleware are run in reverse order during the response phase, which\nincludes <code class=\"docutils literal notranslate\"><span class=\"pre\">process_template_response()</span></code>.</p>\n</section>\n<section id=\"process-response\">\n<span id=\"response-middleware\"></span><h3><code class=\"docutils literal notranslate\"><span class=\"pre\">process_response()</span></code><a class=\"heading-anchor\" href=\"#process-response\"><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=\"process_response\">\n<span class=\"sig-name descname\"><span class=\"pre\">process_response</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">request</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">response</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#process_response\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">request</span></code> is an <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> object. <code class=\"docutils literal notranslate\"><span class=\"pre\">response</span></code> is\nthe <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> or\n<a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a> object returned by a Django view\nor by a middleware.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">process_response()</span></code> is called on all responses before they’re returned to\nthe browser.</p>\n<p>It must return an <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> or\n<a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a> object. It could alter the given\n<code class=\"docutils literal notranslate\"><span class=\"pre\">response</span></code>, or it could create and return a brand-new\n<a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> or\n<a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#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<p>Unlike the <code class=\"docutils literal notranslate\"><span class=\"pre\">process_request()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">process_view()</span></code> methods, the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">process_response()</span></code> method is always called, even if the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">process_request()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">process_view()</span></code> methods of the same middleware\nclass were skipped (because an earlier middleware method returned an\n<a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a>). In particular, this means that your\n<code class=\"docutils literal notranslate\"><span class=\"pre\">process_response()</span></code> method cannot rely on setup done in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">process_request()</span></code>.</p>\n<p>Finally, remember that during the response phase, middleware are applied in\nreverse order, from the bottom up. This means classes defined at the end of\n<a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-MIDDLEWARE_CLASSES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code></a> will be run first.</p>\n<section id=\"dealing-with-streaming-responses\">\n<h4>Dealing with streaming responses<a class=\"heading-anchor\" href=\"#dealing-with-streaming-responses\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Unlike <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a>,\n<a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.StreamingHttpResponse\" title=\"django.http.StreamingHttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse</span></code></a> does not have a <code class=\"docutils literal notranslate\"><span class=\"pre\">content</span></code>\nattribute. As a result, middleware can no longer assume that all responses\nwill have a <code class=\"docutils literal notranslate\"><span class=\"pre\">content</span></code> attribute. If they need access to the content, they\nmust test for streaming responses and adjust their behavior accordingly:</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\">response</span><span class=\"o\">.</span><span class=\"n\">streaming</span><span class=\"p\">:</span>\n    <span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">streaming_content</span> <span class=\"o\">=</span> <span class=\"n\">wrap_streaming_content</span><span class=\"p\">(</span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">streaming_content</span><span class=\"p\">)</span>\n<span class=\"k\">else</span><span class=\"p\">:</span>\n    <span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">content</span> <span class=\"o\">=</span> <span class=\"n\">alter_content</span><span class=\"p\">(</span><span class=\"n\">response</span><span class=\"o\">.</span><span class=\"n\">content</span><span class=\"p\">)</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">streaming_content</span></code> should be assumed to be too large to hold in memory.\nResponse middleware may wrap it in a new generator, but must not consume\nit. Wrapping is typically implemented as follows:</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\">def</span><span class=\"w\"> </span><span class=\"nf\">wrap_streaming_content</span><span class=\"p\">(</span><span class=\"n\">content</span><span class=\"p\">):</span>\n    <span class=\"k\">for</span> <span class=\"n\">chunk</span> <span class=\"ow\">in</span> <span class=\"n\">content</span><span class=\"p\">:</span>\n        <span class=\"k\">yield</span> <span class=\"n\">alter_content</span><span class=\"p\">(</span><span class=\"n\">chunk</span><span class=\"p\">)</span>\n</code></pre></div>\n</aside>\n</section>\n</section>\n<section id=\"process-exception\">\n<span id=\"exception-middleware\"></span><h3><code class=\"docutils literal notranslate\"><span class=\"pre\">process_exception()</span></code><a class=\"heading-anchor\" href=\"#process-exception\"><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=\"process_exception\">\n<span class=\"sig-name descname\"><span class=\"pre\">process_exception</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">request</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">exception</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#process_exception\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">request</span></code> is an <a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpRequest</span></code></a> object. <code class=\"docutils literal notranslate\"><span class=\"pre\">exception</span></code> is an\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Exception</span></code> object raised by the view function.</p>\n<p>Django calls <code class=\"docutils literal notranslate\"><span class=\"pre\">process_exception()</span></code> when a view raises an exception.\n<code class=\"docutils literal notranslate\"><span class=\"pre\">process_exception()</span></code> should return either <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> or an\n<a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> object. If it returns an\n<a class=\"reference internal\" href=\"/es/1.9/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> object, the template response and response\nmiddleware will be applied, and the resulting response returned to the\nbrowser. Otherwise, default exception handling kicks in.</p>\n<p>Again, middleware are run in reverse order during the response phase, which\nincludes <code class=\"docutils literal notranslate\"><span class=\"pre\">process_exception</span></code>. If an exception middleware returns a response,\nthe middleware classes above that middleware will not be called at all.</p>\n</section>\n<section id=\"init\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">__init__()</span></code><a class=\"heading-anchor\" href=\"#init\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Most middleware classes won’t need an initializer since middleware classes are\nessentially placeholders for the <code class=\"docutils literal notranslate\"><span class=\"pre\">process_*</span></code> methods. If you do need some\nglobal state you may use <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__</span></code> to set up. However, keep in mind a couple\nof caveats:</p>\n<ul class=\"simple\">\n<li><p>Django initializes your middleware without any arguments, so you can’t\ndefine <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__</span></code> as requiring any arguments.</p></li>\n<li><p>Unlike the <code class=\"docutils literal notranslate\"><span class=\"pre\">process_*</span></code> methods which get called once per request,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">__init__</span></code> gets called only <em>once</em>, when the Web server responds to the\nfirst request.</p></li>\n</ul>\n<section id=\"marking-middleware-as-unused\">\n<h4>Marking middleware as unused<a class=\"heading-anchor\" href=\"#marking-middleware-as-unused\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>It’s sometimes useful to determine at run-time whether a piece of middleware\nshould be used. In these cases, your middleware’s <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__</span></code> method may\nraise <a class=\"reference internal\" href=\"/es/1.9/ref/exceptions/#django.core.exceptions.MiddlewareNotUsed\" title=\"django.core.exceptions.MiddlewareNotUsed\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">django.core.exceptions.MiddlewareNotUsed</span></code></a>. Django will then remove\nthat piece of middleware from the middleware process and a debug message will\nbe logged to the <code class=\"docutils literal notranslate\"><span class=\"pre\">django.request</span></code> logger when <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> is set to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n<aside class=\"version-note version-changed\" data-version=\"1.8\">\n<p class=\"version-note-title\">Changed in Django 1.8</p><p>Previously, <a class=\"reference internal\" href=\"/es/1.9/ref/exceptions/#django.core.exceptions.MiddlewareNotUsed\" title=\"django.core.exceptions.MiddlewareNotUsed\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">MiddlewareNotUsed</span></code></a> exceptions\nweren’t logged.</p>\n</aside>\n</section>\n</section>\n<section id=\"guidelines\">\n<h3>Guidelines<a class=\"heading-anchor\" href=\"#guidelines\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<ul class=\"simple\">\n<li><p>Middleware classes don’t have to subclass anything.</p></li>\n<li><p>The middleware class can live anywhere on your Python path. All Django\ncares about is that the <a class=\"reference internal\" href=\"/es/1.9/ref/settings/#std-setting-MIDDLEWARE_CLASSES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code></a> setting includes\nthe path to it.</p></li>\n<li><p>Feel free to look at <a class=\"reference internal\" href=\"/es/1.9/ref/middleware/\"><span class=\"doc\">Django’s available middleware</span></a> for examples.</p></li>\n<li><p>If you write a middleware component that you think would be useful to\nother people, contribute to the community! <a class=\"reference internal\" href=\"/es/1.9/internals/contributing/\"><span class=\"doc\">Let us know</span></a>, and we’ll consider adding it to Django.</p></li>\n</ul>\n</section>\n</section>","rootId":"middleware","toc":[{"title":"Activating middleware","anchor":"activating-middleware","children":[]},{"title":"Hooks and application order","anchor":"hooks-and-application-order","children":[]},{"title":"Writing your own middleware","anchor":"writing-your-own-middleware","children":[{"title":"process_request()","anchor":"process-request","children":[]},{"title":"process_view()","anchor":"process-view","children":[]},{"title":"process_template_response()","anchor":"process-template-response","children":[]},{"title":"process_response()","anchor":"process-response","children":[{"title":"Dealing with streaming responses","anchor":"dealing-with-streaming-responses","children":[]}]},{"title":"process_exception()","anchor":"process-exception","children":[]},{"title":"__init__()","anchor":"init","children":[{"title":"Marking middleware as unused","anchor":"marking-middleware-as-unused","children":[]}]},{"title":"Guidelines","anchor":"guidelines","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/es/1.9/topics/"},{"docname":"topics/http/index","title":"Handling HTTP requests","url":"/es/1.9/topics/http/"}],"prev":{"docname":"topics/http/generic-views","title":"Generic views","url":"/es/1.9/topics/http/generic-views/"},"next":{"docname":"topics/http/sessions","title":"How to use sessions","url":"/es/1.9/topics/http/sessions/"},"formats":{"html":"/es/1.9/topics/http/middleware/","markdown":"/es/1.9/topics/http/middleware.md","json":"/es/1.9/topics/http/middleware.json"},"source":"https://github.com/django/django/blob/stable/1.9.x/docs/topics/http/middleware.txt","official":"https://docs.djangoproject.com/es/1.9/topics/http/middleware/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","fr","ja","id","pt-br","es"]}