{"title":"Middleware","version":"6.1","locale":"es","docname":"topics/http/middleware","url":"/es/6.1/topics/http/middleware/","canonical":"https://djangodocs.dev/es/6.1/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/6.1/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/6.1/ref/middleware/\"><span class=\"doc\">built-in\nmiddleware reference</span></a>.</p>\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>A middleware factory is a callable that takes a <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code> callable and\nreturns a middleware. A middleware is a callable that takes a request and\nreturns a response, just like a view.</p>\n<p>A middleware can be written as a function that looks like this:</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\">simple_middleware</span><span class=\"p\">(</span><span class=\"n\">get_response</span><span class=\"p\">):</span>\n    <span class=\"c1\"># One-time configuration and initialization.</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">middleware</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n        <span class=\"c1\"># Code to be executed for each request before</span>\n        <span class=\"c1\"># the view (and later middleware) are called.</span>\n\n        <span class=\"n\">response</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\n        <span class=\"c1\"># Code to be executed for each request/response after</span>\n        <span class=\"c1\"># the view is called.</span>\n\n        <span class=\"k\">return</span> <span class=\"n\">response</span>\n\n    <span class=\"k\">return</span> <span class=\"n\">middleware</span>\n</code></pre></div>\n<p>Or it can be written as a class whose instances are callable, like this:</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\">SimpleMiddleware</span><span class=\"p\">:</span>\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        <span class=\"c1\"># One-time configuration and initialization.</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=\"c1\"># Code to be executed for each request before</span>\n        <span class=\"c1\"># the view (and later middleware) are called.</span>\n\n        <span class=\"n\">response</span> <span class=\"o\">=</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\n        <span class=\"c1\"># Code to be executed for each request/response after</span>\n        <span class=\"c1\"># the view is called.</span>\n\n        <span class=\"k\">return</span> <span class=\"n\">response</span>\n</code></pre></div>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code> callable provided by Django might be the actual view (if\nthis is the last listed middleware) or it might be the next middleware in the\nchain. The current middleware doesn’t need to know or care what exactly it is,\njust that it represents whatever comes next.</p>\n<p>The above is a slight simplification – the <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code> callable for the\nlast middleware in the chain won’t be the actual view but rather a wrapper\nmethod from the handler which takes care of applying <a class=\"reference internal\" href=\"#view-middleware\"><span class=\"std std-ref\">view middleware</span></a>, calling the view with appropriate URL arguments, and\napplying <a class=\"reference internal\" href=\"#template-response-middleware\"><span class=\"std std-ref\">template-response</span></a> and\n<a class=\"reference internal\" href=\"#exception-middleware\"><span class=\"std std-ref\">exception</span></a> middleware.</p>\n<p>Middleware can either support only synchronous Python (the default), only\nasynchronous Python, or both. See <a class=\"reference internal\" href=\"#async-middleware\"><span class=\"std std-ref\">Soporte asíncrono</span></a> for details of how to\nadvertise what you support, and know what kind of request you are getting.</p>\n<p>Middleware can live anywhere on your Python path.</p>\n<section id=\"init-get-response\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">__init__(get_response)</span></code><a class=\"heading-anchor\" href=\"#init-get-response\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Middleware factories must accept a <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code> argument. You can also\ninitialize some global state for the middleware. Keep in mind a couple of\ncaveats:</p>\n<ul class=\"simple\">\n<li><p>Django initializes your middleware with only the <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code> argument,\nso you can’t define <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__()</span></code> as requiring any other arguments.</p></li>\n<li><p>Unlike the <code class=\"docutils literal notranslate\"><span class=\"pre\">__call__()</span></code> method which is called once per request,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">__init__()</span></code> is called only <em>once</em>, when the web server starts.</p></li>\n</ul>\n</section>\n<section id=\"marking-middleware-as-unused\">\n<h3>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></h3>\n<p>It’s sometimes useful to determine at startup time whether a piece of\nmiddleware should be used. In these cases, your middleware’s <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__()</span></code>\nmethod may raise <a class=\"reference internal\" href=\"/es/6.1/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>. Django will\nthen remove that middleware from the middleware process and log a debug message\nto the <a class=\"reference internal\" href=\"/es/6.1/ref/logging/#django-request-logger\"><span class=\"std std-ref\">django.request</span></a> logger when <a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n</section>\n</section>\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 <a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a> list in\nyour Django settings.</p>\n<p>In <a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a>, each middleware component is represented by a string:\nthe full Python path to the middleware factory’s class or function name. For\nexample, here’s the default value created by <a class=\"reference internal\" href=\"/es/6.1/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">django-admin</span>\n<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</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s2\">&quot;django.middleware.security.SecurityMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.contrib.sessions.middleware.SessionMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.middleware.common.CommonMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.middleware.csrf.CsrfViewMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.contrib.auth.middleware.AuthenticationMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.contrib.messages.middleware.MessageMiddleware&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;django.middleware.clickjacking.XFrameOptionsMiddleware&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>A Django installation doesn’t require any middleware — <a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a>\ncan be empty, if you’d like — but it’s strongly suggested that you at least use\n<a class=\"reference internal\" href=\"/es/6.1/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/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a> matters because a middleware can depend on\nother middleware. For instance,\n<a class=\"reference internal\" href=\"/es/6.1/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/6.1/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/6.1/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=\"middleware-order-and-layering\">\n<h2>Middleware order and layering<a class=\"heading-anchor\" href=\"#middleware-order-and-layering\"><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 in\nthe order it’s defined in <a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a>, top-down.</p>\n<p>You can think of it like an onion: each middleware class is a «layer» that\nwraps the view, which is in the core of the onion. If the request passes\nthrough all the layers of the onion (each one calls <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code> to pass\nthe request in to the next layer), all the way to the view at the core, the\nresponse will then pass through every layer (in reverse order) on the way back\nout.</p>\n<p>If one of the layers decides to short-circuit and return a response without\never calling its <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code>, none of the layers of the onion inside that\nlayer (including the view) will see the request or the response. The response\nwill only return through the same layers that the request passed in through.</p>\n</section>\n<section id=\"other-middleware-hooks\">\n<h2>Other middleware hooks<a class=\"heading-anchor\" href=\"#other-middleware-hooks\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Besides the basic request/response middleware pattern described earlier, you\ncan add three other special methods to class-based middleware:</p>\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/6.1/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/6.1/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/6.1/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 the appropriate view; it’ll apply response middleware to that\n<a class=\"reference internal\" href=\"/es/6.1/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/6.1/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 before the view runs or in <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/6.1/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/6.1/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/6.1/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/6.1/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-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/6.1/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/6.1/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/6.1/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, <a class=\"reference internal\" href=\"/es/6.1/ref/views/#error-views\"><span class=\"std std-ref\">default exception handling</span></a> 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 <code class=\"docutils literal notranslate\"><span class=\"pre\">process_exception</span></code> methods of the middleware classes above that\nmiddleware won’t be called at all.</p>\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/6.1/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/6.1/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/6.1/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/6.1/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>\n<section id=\"dealing-with-streaming-responses\">\n<h2>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></h2>\n<p>Unlike <a class=\"reference internal\" href=\"/es/6.1/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/6.1/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<p><a class=\"reference internal\" href=\"/es/6.1/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> allows both synchronous and\nasynchronous iterators. The wrapping function must match. Check\n<a class=\"reference internal\" href=\"/es/6.1/ref/request-response/#django.http.StreamingHttpResponse.is_async\" title=\"django.http.StreamingHttpResponse.is_async\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">StreamingHttpResponse.is_async</span></code></a> if your middleware needs to\nsupport both types of iterator.</p>\n</section>\n<section id=\"exception-handling\">\n<h2>Exception handling<a class=\"heading-anchor\" href=\"#exception-handling\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django automatically converts exceptions raised by the view or by middleware\ninto an appropriate HTTP response with an error status code. <a class=\"reference internal\" href=\"/es/6.1/ref/views/#error-views\"><span class=\"std std-ref\">Certain\nexceptions</span></a> are converted to 4xx status codes, while an unknown\nexception is converted to a 500 status code.</p>\n<p>This conversion takes place before and after each middleware (you can think of\nit as the thin film in between each layer of the onion), so that every\nmiddleware can always rely on getting some kind of HTTP response back from\ncalling its <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code> callable. Middleware don’t need to worry about\nwrapping their call to <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code> in a <code class=\"docutils literal notranslate\"><span class=\"pre\">try/except</span></code> and handling an\nexception that might have been raised by a later middleware or the view. Even\nif the very next middleware in the chain raises an\n<a class=\"reference internal\" href=\"/es/6.1/topics/http/views/#django.http.Http404\" title=\"django.http.Http404\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Http404</span></code></a> exception, for example, your middleware won’t see\nthat exception; instead it will get an <a class=\"reference internal\" href=\"/es/6.1/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 with a <a class=\"reference internal\" href=\"/es/6.1/ref/request-response/#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> of 404.</p>\n<p>You can set <a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-DEBUG_PROPAGATE_EXCEPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG_PROPAGATE_EXCEPTIONS</span></code></a> to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> to skip this\nconversion and propagate exceptions upward.</p>\n</section>\n<section id=\"asynchronous-support\">\n<span id=\"async-middleware\"></span><h2>Soporte asíncrono<a class=\"heading-anchor\" href=\"#asynchronous-support\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Middleware can support any combination of synchronous and asynchronous\nrequests. Django will adapt requests to fit the middleware’s requirements if it\ncannot support both, but at a performance penalty.</p>\n<p>By default, Django assumes that your middleware is capable of handling only\nsynchronous requests. To change these assumptions, set the following attributes\non your middleware factory function or class:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">sync_capable</span></code> is a boolean indicating if the middleware can handle\nsynchronous requests. Defaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">async_capable</span></code> is a boolean indicating if the middleware can handle\nasynchronous requests. Defaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>.</p></li>\n</ul>\n<p>If your middleware has both <code class=\"docutils literal notranslate\"><span class=\"pre\">sync_capable</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">async_capable</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code>, then Django will pass it the request without\nconverting it. In this case, you can work out if your middleware will receive\nasync requests by checking if the <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code> object you are passed is a\ncoroutine function, using <code class=\"docutils literal notranslate\"><span class=\"pre\">inspect.iscoroutinefunction</span></code>.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.decorators</span></code> module contains\n<a class=\"reference internal\" href=\"/es/6.1/ref/utils/#django.utils.decorators.sync_only_middleware\" title=\"django.utils.decorators.sync_only_middleware\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">sync_only_middleware()</span></code></a>,\n<a class=\"reference internal\" href=\"/es/6.1/ref/utils/#django.utils.decorators.async_only_middleware\" title=\"django.utils.decorators.async_only_middleware\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">async_only_middleware()</span></code></a>, and\n<a class=\"reference internal\" href=\"/es/6.1/ref/utils/#django.utils.decorators.sync_and_async_middleware\" title=\"django.utils.decorators.sync_and_async_middleware\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">sync_and_async_middleware()</span></code></a> decorators that\nallow you to apply these flags to middleware factory functions.</p>\n<p>The returned callable must match the sync or async nature of the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code> method. If you have an asynchronous <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code>, you must\nreturn a coroutine function (<code class=\"docutils literal notranslate\"><span class=\"pre\">async</span> <span class=\"pre\">def</span></code>).</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">process_view</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">process_template_response</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">process_exception</span></code>\nmethods, if they are provided, should also be adapted to match the sync/async\nmode. However, Django will individually adapt them as required if you do not,\nat an additional performance penalty.</p>\n<p>Here’s an example of how to create a middleware function that supports both:</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\">inspect</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">iscoroutinefunction</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils.decorators</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">sync_and_async_middleware</span>\n\n\n<span class=\"nd\">@sync_and_async_middleware</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">simple_middleware</span><span class=\"p\">(</span><span class=\"n\">get_response</span><span class=\"p\">):</span>\n    <span class=\"c1\"># One-time configuration and initialization goes here.</span>\n    <span class=\"k\">if</span> <span class=\"n\">iscoroutinefunction</span><span class=\"p\">(</span><span class=\"n\">get_response</span><span class=\"p\">):</span>\n\n        <span class=\"k\">async</span> <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">middleware</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n            <span class=\"c1\"># Do something here!</span>\n            <span class=\"n\">response</span> <span class=\"o\">=</span> <span class=\"k\">await</span> <span class=\"n\">get_response</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">)</span>\n            <span class=\"k\">return</span> <span class=\"n\">response</span>\n\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n\n        <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">middleware</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n            <span class=\"c1\"># Do something here!</span>\n            <span class=\"n\">response</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            <span class=\"k\">return</span> <span class=\"n\">response</span>\n\n    <span class=\"k\">return</span> <span class=\"n\">middleware</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>If you declare a hybrid middleware that supports both synchronous and\nasynchronous calls, the kind of call you get may not match the underlying\nview. Django will optimize the middleware call stack to have as few\nsync/async transitions as possible.</p>\n<p>Thus, even if you are wrapping an async view, you may be called in sync\nmode if there is other, synchronous middleware between you and the view.</p>\n</aside>\n<p>When using an asynchronous class-based middleware, you must ensure that\ninstances are correctly marked as coroutine functions:</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\">inspect</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">iscoroutinefunction</span><span class=\"p\">,</span> <span class=\"n\">markcoroutinefunction</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">AsyncMiddleware</span><span class=\"p\">:</span>\n    <span class=\"n\">async_capable</span> <span class=\"o\">=</span> <span class=\"kc\">True</span>\n    <span class=\"n\">sync_capable</span> <span class=\"o\">=</span> <span class=\"kc\">False</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        <span class=\"k\">if</span> <span class=\"n\">iscoroutinefunction</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">get_response</span><span class=\"p\">):</span>\n            <span class=\"n\">markcoroutinefunction</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span>\n\n    <span class=\"k\">async</span> <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=\"n\">response</span> <span class=\"o\">=</span> <span class=\"k\">await</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        <span class=\"c1\"># Some logic ...</span>\n        <span class=\"k\">return</span> <span class=\"n\">response</span>\n</code></pre></div>\n</section>\n<section id=\"upgrading-pre-django-1-10-style-middleware\">\n<span id=\"upgrading-middleware\"></span><h2>Upgrading pre-Django 1.10-style middleware<a class=\"heading-anchor\" href=\"#upgrading-pre-django-1-10-style-middleware\"><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.utils.deprecation.MiddlewareMixin\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-prename descclassname\"><span class=\"pre\">django.utils.deprecation.</span></span><span class=\"sig-name descname\"><span class=\"pre\">MiddlewareMixin</span></span><a class=\"heading-anchor\" href=\"#django.utils.deprecation.MiddlewareMixin\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Django provides <code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.deprecation.MiddlewareMixin</span></code> to ease creating\nmiddleware classes that are compatible with both <a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a> and the\nold <code class=\"docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code>, and support synchronous and asynchronous requests.\nAll middleware classes included with Django are compatible with both settings.</p>\n<p>The mixin provides an <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__()</span></code> method that requires a <code class=\"docutils literal notranslate\"><span class=\"pre\">get_response</span></code>\nargument and stores it in <code class=\"docutils literal notranslate\"><span class=\"pre\">self.get_response</span></code>.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">__call__()</span></code> method:</p>\n<ol class=\"arabic simple\">\n<li><p>Calls <code class=\"docutils literal notranslate\"><span class=\"pre\">self.process_request(request)</span></code> (if defined).</p></li>\n<li><p>Calls <code class=\"docutils literal notranslate\"><span class=\"pre\">self.get_response(request)</span></code> to get the response from later\nmiddleware and the view.</p></li>\n<li><p>Calls <code class=\"docutils literal notranslate\"><span class=\"pre\">self.process_response(request,</span> <span class=\"pre\">response)</span></code> (if defined).</p></li>\n<li><p>Returns the response.</p></li>\n</ol>\n<p>If used with <code class=\"docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code>, the <code class=\"docutils literal notranslate\"><span class=\"pre\">__call__()</span></code> method will\nnever be used; Django calls <code class=\"docutils literal notranslate\"><span class=\"pre\">process_request()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">process_response()</span></code>\ndirectly.</p>\n<p>In most cases, inheriting from this mixin will be sufficient to make an\nold-style middleware compatible with the new system with sufficient\nbackwards-compatibility. The new short-circuiting semantics will be harmless or\neven beneficial to the existing middleware. In a few cases, a middleware class\nmay need some changes to adjust to the new semantics.</p>\n<p>These are the behavioral differences between using <a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code>:</p>\n<ol class=\"arabic simple\">\n<li><p>Under <code class=\"docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code>, every middleware will always have its\n<code class=\"docutils literal notranslate\"><span class=\"pre\">process_response</span></code> method called, even if an earlier middleware\nshort-circuited by returning a response from its <code class=\"docutils literal notranslate\"><span class=\"pre\">process_request</span></code>\nmethod. Under <a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a>, middleware behaves more like an onion:\nthe layers that a response goes through on the way out are the same layers\nthat saw the request on the way in. If a middleware short-circuits, only\nthat middleware and the ones before it in <a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a> will see the\nresponse.</p></li>\n<li><p>Under <code class=\"docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">process_exception</span></code> is applied to\nexceptions raised from a middleware <code class=\"docutils literal notranslate\"><span class=\"pre\">process_request</span></code> method. Under\n<a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a>, <code class=\"docutils literal notranslate\"><span class=\"pre\">process_exception</span></code> applies only to exceptions\nraised from the view (or from the <code class=\"docutils literal notranslate\"><span class=\"pre\">render</span></code> method of a\n<a class=\"reference internal\" href=\"/es/6.1/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>). Exceptions raised from\na middleware are converted to the appropriate HTTP response and then passed\nto the next middleware.</p></li>\n<li><p>Under <code class=\"docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE_CLASSES</span></code>, if a <code class=\"docutils literal notranslate\"><span class=\"pre\">process_response</span></code> method raises\nan exception, the <code class=\"docutils literal notranslate\"><span class=\"pre\">process_response</span></code> methods of all earlier middleware are\nskipped and a <code class=\"docutils literal notranslate\"><span class=\"pre\">500</span> <span class=\"pre\">Internal</span> <span class=\"pre\">Server</span> <span class=\"pre\">Error</span></code> HTTP response is always\nreturned (even if the exception raised was e.g. an\n<a class=\"reference internal\" href=\"/es/6.1/topics/http/views/#django.http.Http404\" title=\"django.http.Http404\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Http404</span></code></a>). Under <a class=\"reference internal\" href=\"/es/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a>, an exception\nraised from a middleware will immediately be converted to the appropriate\nHTTP response, and then the next middleware in line will see that\nresponse. Middleware are never skipped due to a middleware raising an\nexception.</p></li>\n</ol>\n</section>","rootId":"middleware","toc":[{"title":"Writing your own middleware","anchor":"writing-your-own-middleware","children":[{"title":"__init__(get_response)","anchor":"init-get-response","children":[]},{"title":"Marking middleware as unused","anchor":"marking-middleware-as-unused","children":[]}]},{"title":"Activating middleware","anchor":"activating-middleware","children":[]},{"title":"Middleware order and layering","anchor":"middleware-order-and-layering","children":[]},{"title":"Other middleware hooks","anchor":"other-middleware-hooks","children":[{"title":"process_view()","anchor":"process-view","children":[]},{"title":"process_exception()","anchor":"process-exception","children":[]},{"title":"process_template_response()","anchor":"process-template-response","children":[]}]},{"title":"Dealing with streaming responses","anchor":"dealing-with-streaming-responses","children":[]},{"title":"Exception handling","anchor":"exception-handling","children":[]},{"title":"Soporte asíncrono","anchor":"asynchronous-support","children":[]},{"title":"Upgrading pre-Django 1.10-style middleware","anchor":"upgrading-pre-django-1-10-style-middleware","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/es/6.1/topics/"},{"docname":"topics/http/index","title":"Handling HTTP requests","url":"/es/6.1/topics/http/"}],"prev":{"docname":"topics/http/generic-views","title":"Generic views","url":"/es/6.1/topics/http/generic-views/"},"next":{"docname":"topics/http/sessions","title":"How to use sessions","url":"/es/6.1/topics/http/sessions/"},"formats":{"html":"/es/6.1/topics/http/middleware/","markdown":"/es/6.1/topics/http/middleware.md","json":"/es/6.1/topics/http/middleware.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/topics/http/middleware.txt","official":"https://docs.djangoproject.com/es/6.1/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","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}