{"title":"Tampilan Siap pakai","version":"1.10","locale":"id","docname":"ref/views","url":"/id/1.10/ref/views/","canonical":"https://djangodocs.dev/id/1.10/ref/views/","summary":"Several of Django's built-in views are documented in Menulis tampilan as well as elsewhere in the documentation. Melayani berkas di pengembangan Link to this…","html":"<span id=\"built-in-views\"></span><h1>Tampilan Siap pakai<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=\"/id/1.10/topics/http/views/\"><span class=\"doc\">Menulis tampilan</span></a> as well as elsewhere in the documentation.</p>\n<section id=\"serving-files-in-development\">\n<h2>Melayani berkas di pengembangan<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=\"/id/1.10/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=\"/id/1.10/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.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\">url</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=\"/id/1.10/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=\"/id/1.10/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=\"/id/1.10/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>Tampilan kesalahan<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=\"/id/1.10/topics/http/views/#customizing-error-views\"><span class=\"std std-ref\">Menyesuaikan tampilan kesalahan</span></a>.</p>\n<section id=\"the-404-page-not-found-view\">\n<span id=\"http-not-found-view\"></span><h3>Tampilan 404 (halaman tidak ditemukan)<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=\"/id/1.10/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 very\nsimple &quot;Not Found&quot; message or loads and renders the template <code class=\"docutils literal notranslate\"><span class=\"pre\">404.html</span></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\"><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>Tiga hal untuk dicatat tentang tampilan 404:</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=\"/id/1.10/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=\"/id/1.10/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<aside class=\"version-note version-changed\" data-version=\"1.9\">\n<p class=\"version-note-title\">Changed in Django 1.9</p><p>The signature of <code class=\"docutils literal notranslate\"><span class=\"pre\">page_not_found()</span></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<aside class=\"version-note version-changed\" data-version=\"1.10\">\n<p class=\"version-note-title\">Changed in Django 1.10</p><p>Passing a nonexistent <code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code> will raise <code class=\"docutils literal notranslate\"><span class=\"pre\">TemplateDoesNotExist</span></code>.</p>\n</aside>\n</section>\n<section id=\"the-500-server-error-view\">\n<span id=\"http-internal-server-error-view\"></span><h3>Tampilan 500 (kesalahan peladen)<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 very\nsimple &quot;Server Error&quot; message or loads and renders the template <code class=\"docutils literal notranslate\"><span class=\"pre\">500.html</span></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\"><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=\"/id/1.10/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<aside class=\"version-note version-changed\" data-version=\"1.10\">\n<p class=\"version-note-title\">Changed in Django 1.10</p><p>Passing a nonexistent <code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code> will raise <code class=\"docutils literal notranslate\"><span class=\"pre\">TemplateDoesNotExist</span></code>.</p>\n</aside>\n</section>\n<section id=\"the-403-http-forbidden-view\">\n<span id=\"http-forbidden-view\"></span><h3>Tampilan 403 (Terlarang HTTP)<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&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\"><span class=\"pre\">exception</span></code>, which is the unicode\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=\"/id/1.10/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<aside class=\"version-note version-changed\" data-version=\"1.9\">\n<p class=\"version-note-title\">Changed in Django 1.9</p><p>The signature of <code class=\"docutils literal notranslate\"><span class=\"pre\">permission_denied()</span></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<aside class=\"version-note version-changed\" data-version=\"1.10\">\n<p class=\"version-note-title\">Changed in Django 1.10</p><p>Passing a nonexistent <code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code> will raise <code class=\"docutils literal notranslate\"><span class=\"pre\">TemplateDoesNotExist</span></code>.</p>\n</aside>\n</section>\n<section id=\"the-400-bad-request-view\">\n<span id=\"http-bad-request-view\"></span><h3>Tampilan (permintaan buruk) 400<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>Ketika sebuah <a class=\"reference internal\" href=\"/id/1.10/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> dimunculkan di Django, dia mungkin ditangani oleh komponen dari Django (sebagai contoh mengatur kembali data sesi). Jika tidak secara khusus ditangani, Django akan memperimbangkan permintaan saat ini 'bad request'  daripada kesalahan peladen.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.views.defaults.bad_request</span></code>, jika tidak sangat mirip pada tampilan <code class=\"docutils literal notranslate\"><span class=\"pre\">server_error</span></code>, tetapi mengembalikan dengan kode keadaan 400 yang kondisi kesalahan adalah hasil dari pengerjaan klien. Secara awal, tidak ada terhubung pada pengecualian yang memicu tampilan dilewatkan ke konteks cetakan, sebagai pesan pengecualian mungkin mengandung informasi sensitif seperti jalur sistem berkas.</p>\n<p>Tampilan <code class=\"docutils literal notranslate\"><span class=\"pre\">bad_request</span></code> juga hanya digunakan ketika <a class=\"reference internal\" href=\"/id/1.10/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> adalah <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>.</p>\n<aside class=\"version-note version-changed\" data-version=\"1.9\">\n<p class=\"version-note-title\">Changed in Django 1.9</p><p>Tanda tangan dari <code class=\"docutils literal notranslate\"><span class=\"pre\">bad_request()</span></code> berubah di Django 1.9. Fungsi sekarang menerima parameter kedua, pengecualian yang membangkitkan kesalahan.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"1.10\">\n<p class=\"version-note-title\">Changed in Django 1.10</p><p>Passing a nonexistent <code class=\"docutils literal notranslate\"><span class=\"pre\">template_name</span></code> will raise <code class=\"docutils literal notranslate\"><span class=\"pre\">TemplateDoesNotExist</span></code>.</p>\n</aside>\n</section>\n</section>","rootId":"module-django.views","toc":[{"title":"Melayani berkas di pengembangan","anchor":"serving-files-in-development","children":[]},{"title":"Tampilan kesalahan","anchor":"error-views","children":[{"title":"Tampilan 404 (halaman tidak ditemukan)","anchor":"the-404-page-not-found-view","children":[]},{"title":"Tampilan 500 (kesalahan peladen)","anchor":"the-500-server-error-view","children":[]},{"title":"Tampilan 403 (Terlarang HTTP)","anchor":"the-403-http-forbidden-view","children":[]},{"title":"Tampilan (permintaan buruk) 400","anchor":"the-400-bad-request-view","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"Acuan API","url":"/id/1.10/ref/"}],"prev":{"docname":"ref/validators","title":"Pengesah","url":"/id/1.10/ref/validators/"},"next":{"docname":"misc/index","title":"Dokumentasi-meta dan bunga rampai","url":"/id/1.10/misc/"},"formats":{"html":"/id/1.10/ref/views/","markdown":"/id/1.10/ref/views.md","json":"/id/1.10/ref/views.json"},"source":"https://github.com/django/django/blob/stable/1.10.x/docs/ref/views.txt","official":"https://docs.djangoproject.com/id/1.10/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","el","pl"]}