{"title":"Built-in Views","version":"3.2","locale":"es","docname":"ref/views","url":"/es/3.2/ref/views/","canonical":"https://djangodocs.dev/es/3.2/ref/views/","summary":"Several of Django’s built-in views are documented in Writing views as well as elsewhere in the documentation. Serving files in development Link to this heading #…","html":"<span id=\"built-in-views\"></span><h1>Built-in Views<a class=\"heading-anchor\" href=\"#module-django.views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Several of Django’s built-in views are documented in\n<a class=\"reference internal\" href=\"/es/3.2/topics/http/views/\"><span class=\"doc\">Writing views</span></a> as well as elsewhere in the documentation.</p>\n<section id=\"serving-files-in-development\">\n<h2>Serving files in development<a class=\"heading-anchor\" href=\"#serving-files-in-development\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.static.serve\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">static.</span></span><span class=\"sig-name descname\"><span class=\"pre\">serve</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\">path</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">document_root</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">show_indexes</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.static.serve\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>There may be files other than your project’s static assets that, for\nconvenience, you’d like to have Django serve for you in local development.\nThe <a class=\"reference internal\" href=\"#django.views.static.serve\" title=\"django.views.static.serve\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">serve()</span></code></a> view can be used to serve any directory\nyou give it. (This view is <strong>not</strong> hardened for production use and should be\nused only as a development aid; you should serve these files in production\nusing a real front-end web server).</p>\n<p>The most likely example is user-uploaded content in <a class=\"reference internal\" href=\"/es/3.2/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_ROOT</span></code></a>.\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code> is intended for static assets and has no\nbuilt-in handling for user-uploaded files, but you can have Django serve your\n<a class=\"reference internal\" href=\"/es/3.2/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_ROOT</span></code></a> by appending something like this to your URLconf:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">settings</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.urls</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">re_path</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.views.static</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">serve</span>\n\n<span class=\"c1\"># ... the rest of your URLconf goes here ...</span>\n\n<span class=\"k\">if</span> <span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">DEBUG</span><span class=\"p\">:</span>\n    <span class=\"n\">urlpatterns</span> <span class=\"o\">+=</span> <span class=\"p\">[</span>\n        <span class=\"n\">re_path</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^media/(?P&lt;path&gt;.*)$&#39;</span><span class=\"p\">,</span> <span class=\"n\">serve</span><span class=\"p\">,</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;document_root&#39;</span><span class=\"p\">:</span> <span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">MEDIA_ROOT</span><span class=\"p\">,</span>\n        <span class=\"p\">}),</span>\n    <span class=\"p\">]</span>\n</code></pre></div>\n<p>Note, the snippet assumes your <a class=\"reference internal\" href=\"/es/3.2/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a> has a value of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'/media/'</span></code>. This will call the <a class=\"reference internal\" href=\"#django.views.static.serve\" title=\"django.views.static.serve\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">serve()</span></code></a> view,\npassing in the path from the URLconf and the (required) <code class=\"docutils literal notranslate\"><span class=\"pre\">document_root</span></code>\nparameter.</p>\n<p>Since it can become a bit cumbersome to define this URL pattern, Django\nships with a small URL helper function <a class=\"reference internal\" href=\"/es/3.2/ref/urls/#django.conf.urls.static.static\" title=\"django.conf.urls.static.static\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">static()</span></code></a>\nthat takes as parameters the prefix such as <a class=\"reference internal\" href=\"/es/3.2/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a> and a dotted\npath to a view, such as <code class=\"docutils literal notranslate\"><span class=\"pre\">'django.views.static.serve'</span></code>. Any other function\nparameter will be transparently passed to the view.</p>\n</section>\n<section id=\"error-views\">\n<span id=\"id1\"></span><h2>Error views<a class=\"heading-anchor\" href=\"#error-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django comes with a few views by default for handling HTTP errors. To override\nthese with your own custom views, see <a class=\"reference internal\" href=\"/es/3.2/topics/http/views/#customizing-error-views\"><span class=\"std std-ref\">Customizing error views</span></a>.</p>\n<section id=\"the-404-page-not-found-view\">\n<span id=\"http-not-found-view\"></span><h3>The 404 (page not found) view<a class=\"heading-anchor\" href=\"#the-404-page-not-found-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.defaults.page_not_found\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">defaults.</span></span><span class=\"sig-name descname\"><span class=\"pre\">page_not_found</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>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">template_name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'404.html'</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.defaults.page_not_found\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>When you raise <a class=\"reference internal\" href=\"/es/3.2/topics/http/views/#django.http.Http404\" title=\"django.http.Http404\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">Http404</span></code></a> from within a view, Django loads a\nspecial view devoted to handling 404 errors. By default, it’s the view\n<a class=\"reference internal\" href=\"#django.views.defaults.page_not_found\" title=\"django.views.defaults.page_not_found\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.views.defaults.page_not_found()</span></code></a>, which either produces a «Not\nFound» message or loads and renders the template <code class=\"docutils literal notranslate\"><span class=\"pre\">404.html</span></code> if you created it\nin your root template directory.</p>\n<p>The default 404 view will pass two variables to the template: <code class=\"docutils literal notranslate\"><span class=\"pre\">request_path</span></code>,\nwhich is the URL that resulted in the error, and <code class=\"docutils literal notranslate\"><span class=\"pre\">exception</span></code>, which is a\nuseful representation of the exception that triggered the view (e.g. containing\nany message passed to a specific <code class=\"docutils literal notranslate\"><span class=\"pre\">Http404</span></code> instance).</p>\n<p>Three things to note about 404 views:</p>\n<ul class=\"simple\">\n<li><p>The 404 view is also called if Django doesn’t find a match after\nchecking every regular expression in the URLconf.</p></li>\n<li><p>The 404 view is passed a <a class=\"reference internal\" href=\"/es/3.2/ref/templates/api/#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RequestContext</span></code></a> and\nwill have access to variables supplied by your template context\nprocessors (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code>).</p></li>\n<li><p>If <a class=\"reference internal\" href=\"/es/3.2/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> is set to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> (in your settings module), then\nyour 404 view will never be used, and your URLconf will be displayed\ninstead, with some debug information.</p></li>\n</ul>\n</section>\n<section id=\"the-500-server-error-view\">\n<span id=\"http-internal-server-error-view\"></span><h3>The 500 (server error) view<a class=\"heading-anchor\" href=\"#the-500-server-error-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.defaults.server_error\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">defaults.</span></span><span class=\"sig-name descname\"><span class=\"pre\">server_error</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\">template_name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'500.html'</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.defaults.server_error\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Similarly, Django executes special-case behavior in the case of runtime errors\nin view code. If a view results in an exception, Django will, by default, call\nthe view <code class=\"docutils literal notranslate\"><span class=\"pre\">django.views.defaults.server_error</span></code>, which either produces a\n«Server Error» message or loads and renders the template <code class=\"docutils literal notranslate\"><span class=\"pre\">500.html</span></code> if you\ncreated it in your root template directory.</p>\n<p>The default 500 view passes no variables to the <code class=\"docutils literal notranslate\"><span class=\"pre\">500.html</span></code> template and is\nrendered with an empty <code class=\"docutils literal notranslate\"><span class=\"pre\">Context</span></code> to lessen the chance of additional errors.</p>\n<p>If <a class=\"reference internal\" href=\"/es/3.2/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> is set to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> (in your settings module), then\nyour 500 view will never be used, and the traceback will be displayed\ninstead, with some debug information.</p>\n</section>\n<section id=\"the-403-http-forbidden-view\">\n<span id=\"http-forbidden-view\"></span><h3>The 403 (HTTP Forbidden) view<a class=\"heading-anchor\" href=\"#the-403-http-forbidden-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.defaults.permission_denied\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">defaults.</span></span><span class=\"sig-name descname\"><span class=\"pre\">permission_denied</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>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">template_name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'403.html'</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.defaults.permission_denied\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>In the same vein as the 404 and 500 views, Django has a view to handle 403\nForbidden errors. If a view results in a 403 exception then Django will, by\ndefault, call the view <code class=\"docutils literal notranslate\"><span class=\"pre\">django.views.defaults.permission_denied</span></code>.</p>\n<p>This view loads and renders the template <code class=\"docutils literal notranslate\"><span class=\"pre\">403.html</span></code> in your root template\ndirectory, or if this file does not exist, instead serves the text\n«403 Forbidden», as per <span class=\"target\" id=\"index-0\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc7231.html#section-6.5.3\"><strong>RFC 7231 Section 6.5.3</strong></a> (the HTTP 1.1 Specification).\nThe template context contains <code class=\"docutils literal notranslate\"><span class=\"pre\">exception</span></code>, which is the string\nrepresentation of the exception that triggered the view.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.views.defaults.permission_denied</span></code> is triggered by a\n<a class=\"reference internal\" href=\"/es/3.2/ref/exceptions/#django.core.exceptions.PermissionDenied\" title=\"django.core.exceptions.PermissionDenied\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">PermissionDenied</span></code></a> exception. To deny access in a\nview you can use code 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=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.exceptions</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">PermissionDenied</span>\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">edit</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"n\">pk</span><span class=\"p\">):</span>\n    <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">user</span><span class=\"o\">.</span><span class=\"n\">is_staff</span><span class=\"p\">:</span>\n        <span class=\"k\">raise</span> <span class=\"n\">PermissionDenied</span>\n    <span class=\"c1\"># ...</span>\n</code></pre></div>\n</section>\n<section id=\"the-400-bad-request-view\">\n<span id=\"http-bad-request-view\"></span><h3>The 400 (bad request) view<a class=\"heading-anchor\" href=\"#the-400-bad-request-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.defaults.bad_request\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">defaults.</span></span><span class=\"sig-name descname\"><span class=\"pre\">bad_request</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>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">template_name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'400.html'</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.defaults.bad_request\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>When a <a class=\"reference internal\" href=\"/es/3.2/ref/exceptions/#django.core.exceptions.SuspiciousOperation\" title=\"django.core.exceptions.SuspiciousOperation\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">SuspiciousOperation</span></code></a> is raised in Django,\nit may be handled by a component of Django (for example resetting the session\ndata). If not specifically handled, Django will consider the current request a\n“bad request” instead of a server error.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.views.defaults.bad_request</span></code>, is otherwise very similar to the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">server_error</span></code> view, but returns with the status code 400 indicating that\nthe error condition was the result of a client operation. By default, nothing\nrelated to the exception that triggered the view is passed to the template\ncontext, as the exception message might contain sensitive information like\nfilesystem paths.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">bad_request</span></code> views are also only used when <a class=\"reference internal\" href=\"/es/3.2/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\">False</span></code>.</p>\n</section>\n</section>","rootId":"module-django.views","toc":[{"title":"Serving files in development","anchor":"serving-files-in-development","children":[]},{"title":"Error views","anchor":"error-views","children":[{"title":"The 404 (page not found) view","anchor":"the-404-page-not-found-view","children":[]},{"title":"The 500 (server error) view","anchor":"the-500-server-error-view","children":[]},{"title":"The 403 (HTTP Forbidden) view","anchor":"the-403-http-forbidden-view","children":[]},{"title":"The 400 (bad request) view","anchor":"the-400-bad-request-view","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/es/3.2/ref/"}],"prev":{"docname":"ref/validators","title":"Validators","url":"/es/3.2/ref/validators/"},"next":{"docname":"misc/index","title":"Documentación meta y miscelánea","url":"/es/3.2/misc/"},"formats":{"html":"/es/3.2/ref/views/","markdown":"/es/3.2/ref/views.md","json":"/es/3.2/ref/views.json"},"source":"https://github.com/django/django/blob/stable/3.2.x/docs/ref/views.txt","official":"https://docs.djangoproject.com/es/3.2/ref/views/","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","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}