{"title":"Built-in Views","version":"1.9","locale":"ja","docname":"ref/views","url":"/ja/1.9/ref/views/","canonical":"https://djangodocs.dev/ja/1.9/ref/views/","summary":"Several of Django's built-in views are documented in Writing views as well as elsewhere in the documentation.","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=\"/ja/1.9/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\">static.</span><span class=\"sig-name descname\">serve</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em>, <em class=\"sig-param\">path</em>, <em class=\"sig-param\">document_root</em>, <em class=\"sig-param\">show_indexes<span class=\"o\">=</span><span class=\"default_value\">False</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\">serve()</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=\"/ja/1.9/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_ROOT</code></a>.\n<code class=\"docutils literal notranslate\">django.contrib.staticfiles</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=\"/ja/1.9/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_ROOT</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=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.static</span> <span class=\"kn\">import</span> serve\n\n<span class=\"c1\"># ... the rest of your URLconf goes here ...</span>\n\n<span class=\"k\">if</span> settings<span class=\"o\">.</span>DEBUG<span class=\"p\">:</span>\n    urlpatterns <span class=\"o\">+=</span> <span class=\"p\">[</span>\n        url<span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;^media/(?P&lt;path&gt;.*)$&#39;</span><span class=\"p\">,</span> serve<span class=\"p\">,</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;document_root&#39;</span><span class=\"p\">:</span> settings<span class=\"o\">.</span>MEDIA_ROOT<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=\"/ja/1.9/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_URL</code></a> has a value of\n<code class=\"docutils literal notranslate\">'/media/'</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\">serve()</code></a> view,\npassing in the path from the URLconf and the (required) <code class=\"docutils literal notranslate\">document_root</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=\"/ja/1.9/ref/urls/#django.conf.urls.static.static\" title=\"django.conf.urls.static.static\"><code class=\"xref py py-func docutils literal notranslate\">static()</code></a>\nthat takes as parameters the prefix such as <a class=\"reference internal\" href=\"/ja/1.9/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_URL</code></a> and a dotted\npath to a view, such as <code class=\"docutils literal notranslate\">'django.views.static.serve'</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=\"/ja/1.9/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\">defaults.</span><span class=\"sig-name descname\">page_not_found</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em>, <em class=\"sig-param\">exception</em>, <em class=\"sig-param\">template_name<span class=\"o\">=</span><span class=\"default_value\">'404.html'</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=\"/ja/1.9/topics/http/views/#django.http.Http404\" title=\"django.http.Http404\"><code class=\"xref py py-exc docutils literal notranslate\">Http404</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\">django.views.defaults.page_not_found()</code></a>, which either produces a very\nsimple &quot;Not Found&quot; message or loads and renders the template <code class=\"docutils literal notranslate\">404.html</code> if\nyou created it in your root template directory.</p>\n<p>The default 404 view will pass two variables to the template: <code class=\"docutils literal notranslate\">request_path</code>,\nwhich is the URL that resulted in the error, and <code class=\"docutils literal notranslate\">exception</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\">Http404</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=\"/ja/1.9/ref/templates/api/#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\">RequestContext</code></a> and\nwill have access to variables supplied by your template context\nprocessors (e.g. <code class=\"docutils literal notranslate\">MEDIA_URL</code>).</p></li>\n<li><p>If <a class=\"reference internal\" href=\"/ja/1.9/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> is set to <code class=\"docutils literal notranslate\">True</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<aside class=\"version-note version-changed\" data-version=\"1.9\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 1.9</p><p>The signature of <code class=\"docutils literal notranslate\">page_not_found()</code> changed. The function now accepts a\nsecond parameter, the exception that triggered the error. A useful\nrepresentation of the exception is also passed in the template context.</p>\n</aside>\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\">defaults.</span><span class=\"sig-name descname\">server_error</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em>, <em class=\"sig-param\">template_name<span class=\"o\">=</span><span class=\"default_value\">'500.html'</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\">django.views.defaults.server_error</code>, which either produces a very\nsimple &quot;Server Error&quot; message or loads and renders the template <code class=\"docutils literal notranslate\">500.html</code> if\nyou created it in your root template directory.</p>\n<p>The default 500 view passes no variables to the <code class=\"docutils literal notranslate\">500.html</code> template and is\nrendered with an empty <code class=\"docutils literal notranslate\">Context</code> to lessen the chance of additional errors.</p>\n<p>If <a class=\"reference internal\" href=\"/ja/1.9/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> is set to <code class=\"docutils literal notranslate\">True</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\">defaults.</span><span class=\"sig-name descname\">permission_denied</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em>, <em class=\"sig-param\">exception</em>, <em class=\"sig-param\">template_name<span class=\"o\">=</span><span class=\"default_value\">'403.html'</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\">django.views.defaults.permission_denied</code>.</p>\n<p>This view loads and renders the template <code class=\"docutils literal notranslate\">403.html</code> in your root template\ndirectory, or if this file does not exist, instead serves the text\n&quot;403 Forbidden&quot;, 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\">exception</code>, which is the unicode\nrepresentation of the exception that triggered the view.</p>\n<p><code class=\"docutils literal notranslate\">django.views.defaults.permission_denied</code> is triggered by a\n<a class=\"reference internal\" href=\"/ja/1.9/ref/exceptions/#django.core.exceptions.PermissionDenied\" title=\"django.core.exceptions.PermissionDenied\"><code class=\"xref py py-exc docutils literal notranslate\">PermissionDenied</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=\"nn\">django.core.exceptions</span> <span class=\"kn\">import</span> PermissionDenied\n\n<span class=\"k\">def</span> <span class=\"nf\">edit</span><span class=\"p\">(</span>request<span class=\"p\">,</span> pk<span class=\"p\">):</span>\n    <span class=\"k\">if</span> <span class=\"ow\">not</span> request<span class=\"o\">.</span>user<span class=\"o\">.</span>is_staff<span class=\"p\">:</span>\n        <span class=\"k\">raise</span> PermissionDenied\n    <span class=\"c1\"># ...</span>\n</code></pre></div>\n<aside class=\"version-note version-changed\" data-version=\"1.9\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 1.9</p><p>The signature of <code class=\"docutils literal notranslate\">permission_denied()</code> changed in Django 1.9. The function\nnow accepts a second parameter, the exception that triggered the error. The\nunicode representation of the exception is also passed in the template\ncontext.</p>\n</aside>\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\">defaults.</span><span class=\"sig-name descname\">bad_request</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em>, <em class=\"sig-param\">exception</em>, <em class=\"sig-param\">template_name<span class=\"o\">=</span><span class=\"default_value\">'400.html'</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=\"/ja/1.9/ref/exceptions/#django.core.exceptions.SuspiciousOperation\" title=\"django.core.exceptions.SuspiciousOperation\"><code class=\"xref py py-exc docutils literal notranslate\">SuspiciousOperation</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\">django.views.defaults.bad_request</code>, is otherwise very similar to the\n<code class=\"docutils literal notranslate\">server_error</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\">bad_request</code> views are also only used when <a class=\"reference internal\" href=\"/ja/1.9/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\">DEBUG</code></a> is <code class=\"docutils literal notranslate\">False</code>.</p>\n<aside class=\"version-note version-changed\" data-version=\"1.9\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 1.9</p><p>The signature of <code class=\"docutils literal notranslate\">bad_request()</code> changed in Django 1.9. The function\nnow accepts a second parameter, the exception that triggered the error.</p>\n</aside>\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":"/ja/1.9/ref/"}],"prev":{"docname":"ref/validators","title":"Validators","url":"/ja/1.9/ref/validators/"},"next":{"docname":"misc/index","title":"Meta-documentation and miscellany","url":"/ja/1.9/misc/"},"formats":{"html":"/ja/1.9/ref/views/","markdown":"/ja/1.9/ref/views.md","json":"/ja/1.9/ref/views.json"},"source":"https://github.com/django/django/blob/stable/1.9.x/docs/ref/views.txt","official":"https://docs.djangoproject.com/ja/1.9/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","fr","ja","id","pt-br","es"]}