{"title":"Tampilan berdasarkan-kelas","version":"6.1","locale":"id","docname":"topics/class-based-views/index","url":"/id/6.1/topics/class-based-views/","canonical":"https://djangodocs.dev/id/6.1/topics/class-based-views/","summary":"A view is a callable which takes a request and returns a response. This can be more than just a function, and Django provides an example of some classes…","html":"<h1>Tampilan berdasarkan-kelas<a class=\"heading-anchor\" href=\"#class-based-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>A view is a callable which takes a request and returns a\nresponse. This can be more than just a function, and Django provides\nan example of some classes which can be used as views. These allow you\nto structure your views and reuse code by harnessing inheritance and\nmixins. There are also some generic views for tasks which we'll get to later,\nbut you may want to design your own structure of reusable views which suits\nyour use case. For full details, see the <a class=\"reference internal\" href=\"/id/6.1/ref/class-based-views/\"><span class=\"doc\">class-based views reference\ndocumentation</span></a>.</p>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/id/6.1/topics/class-based-views/intro/\">Perkenalan ke tampilan berdasarkan-kelas</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/id/6.1/topics/class-based-views/generic-display/\">Tampilan umum berdasarkan kelas siap-pakai</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/id/6.1/topics/class-based-views/generic-editing/\">Menangani formulir dengan tampilan berdasarkan-kelas</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/id/6.1/topics/class-based-views/mixins/\">Menggunakan mixin dengan tampilan berdasarkan-kelas</a></li>\n</ul>\n</div>\n<aside class=\"admonition-there-are-community-maintained-solutions admonition\" role=\"note\">\n<p class=\"admonition-title\">There are community-maintained solutions!</p>\n<p>Django has a vibrant ecosystem. There are generic view extensions\nhighlighted on the <a class=\"reference external\" href=\"https://www.djangoproject.com/community/ecosystem/#forms-views\">Community Ecosystem</a> page that build on Django's\nclass-based views. The Django Packages <a class=\"reference external\" href=\"https://djangopackages.org/grids/g/cbv/\">Class Based Views grid</a> has\neven more options for you!</p>\n</aside>\n<section id=\"basic-examples\">\n<h2>Contoh dasar<a class=\"heading-anchor\" href=\"#basic-examples\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django provides base view classes which will suit a wide range of applications.\nAll views inherit from the <a class=\"reference internal\" href=\"/id/6.1/ref/class-based-views/base/#django.views.generic.base.View\" title=\"django.views.generic.base.View\"><code class=\"xref py py-class docutils literal notranslate\">View</code></a> class,\nwhich handles linking the view into the URLs, HTTP method dispatching and other\ncommon features. <a class=\"reference internal\" href=\"/id/6.1/ref/class-based-views/base/#django.views.generic.base.RedirectView\" title=\"django.views.generic.base.RedirectView\"><code class=\"xref py py-class docutils literal notranslate\">RedirectView</code></a> provides a\nHTTP redirect, and <a class=\"reference internal\" href=\"/id/6.1/ref/class-based-views/base/#django.views.generic.base.TemplateView\" title=\"django.views.generic.base.TemplateView\"><code class=\"xref py py-class docutils literal notranslate\">TemplateView</code></a> extends the\nbase class to make it also render a template.</p>\n</section>\n<section id=\"usage-in-your-urlconf\">\n<h2>Penggunaan dalam URLconf anda<a class=\"heading-anchor\" href=\"#usage-in-your-urlconf\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Cara paling langsung untuk menggunakan tampilan umum adalah membuatnya langsung di URLconf anda. Jika anda hanya mengubah beberapa atribut pada tampilan berbasis kelas, anda dapat meneruskannya ke dalam pemanggilan metode <a class=\"reference internal\" href=\"/id/6.1/ref/class-based-views/base/#django.views.generic.base.View.as_view\" title=\"django.views.generic.base.View.as_view\"><code class=\"xref py py-meth docutils literal notranslate\">as_view()</code></a> itu sendiri:</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.urls</span> <span class=\"kn\">import</span> path\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> TemplateView\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    path<span class=\"p\">(</span><span class=\"s2\">&quot;about/&quot;</span><span class=\"p\">,</span> TemplateView<span class=\"o\">.</span>as_view<span class=\"p\">(</span>template_name<span class=\"o\">=</span><span class=\"s2\">&quot;about.html&quot;</span><span class=\"p\">)),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Apapun argumen dilewatkan ke <a class=\"reference internal\" href=\"/id/6.1/ref/class-based-views/base/#django.views.generic.base.View.as_view\" title=\"django.views.generic.base.View.as_view\"><code class=\"xref py py-meth docutils literal notranslate\">as_view()</code></a> akan menimpa atribut disetel pada kelas. Dalam contoh ini, kami menyetel template_name` pada <code class=\"docutils literal notranslate\">TemplateView</code>. Pola penibanan mirip dapat digunakan untuk atribut <code class=\"docutils literal notranslate\">url</code> pada <a class=\"reference internal\" href=\"/id/6.1/ref/class-based-views/base/#django.views.generic.base.RedirectView\" title=\"django.views.generic.base.RedirectView\"><code class=\"xref py py-class docutils literal notranslate\">RedirectView</code></a>.</p>\n</section>\n<section id=\"subclassing-generic-views\">\n<h2>Mensubkelaskan tampilan umum<a class=\"heading-anchor\" href=\"#subclassing-generic-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Cara kedua yang lebih ampuh untuk menggunakan tampilan umum adalah mewarisi dari tampilan yang ada dan mengganti atribut (seperti <code class=\"docutils literal notranslate\">template_name</code>) atau metode (seperti <code class=\"docutils literal notranslate\">get_context_data</code>) di subkelas anda untuk memberikan nilai atau metode baru . Perhatikan, misalnya, tampilan yang hanya menampilkan satu template, <code class=\"docutils literal notranslate\">about.html</code>. Django memiliki tampilan umum untuk melakukan ini - <a class=\"reference internal\" href=\"/id/6.1/ref/class-based-views/base/#django.views.generic.base.TemplateView\" title=\"django.views.generic.base.TemplateView\"><code class=\"xref py py-class docutils literal notranslate\">TemplateView</code></a> - jadi kita dapat mensubklasifikasikannya, dan mengganti nama cetakan:</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=\"c1\"># some_app/views.py</span>\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> TemplateView\n\n\n<span class=\"k\">class</span> <span class=\"nc\">AboutView</span><span class=\"p\">(</span>TemplateView<span class=\"p\">):</span>\n    template_name <span class=\"o\">=</span> <span class=\"s2\">&quot;about.html&quot;</span>\n</code></pre></div>\n<p>Kemudian kita perlu menambahkan tampilan baru ini ke dalam URLconf kita. <code class=\"xref py py-class docutils literal notranslate\">TemplateView</code> adalah kelas, bukan fungsi, jadi kami mengarahkan URL ke metode kelas <a class=\"reference internal\" href=\"/id/6.1/ref/class-based-views/base/#django.views.generic.base.View.as_view\" title=\"django.views.generic.base.View.as_view\"><code class=\"xref py py-meth docutils literal notranslate\">as_view()</code></a> , yang menyediakan masukan mirip fungsi ke tampilan berdasarkan kelas</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=\"c1\"># urls.py</span>\n<span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> path\n<span class=\"kn\">from</span> <span class=\"nn\">some_app.views</span> <span class=\"kn\">import</span> AboutView\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    path<span class=\"p\">(</span><span class=\"s2\">&quot;about/&quot;</span><span class=\"p\">,</span> AboutView<span class=\"o\">.</span>as_view<span class=\"p\">()),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Untuk informasi lebih pada bagaimana menggunakan tampilan umum siap pakai, obrolkan topik selanjutnya di <a class=\"reference internal\" href=\"/id/6.1/topics/class-based-views/generic-display/\"><span class=\"doc\">generic class-based views</span></a>.</p>\n<section id=\"supporting-other-http-methods\">\n<span id=\"id1\"></span><h3>mendukung cara HTTP lain<a class=\"heading-anchor\" href=\"#supporting-other-http-methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Misalkan seseorang ingin mengakses pustaka buku kami terhadap HTTP menggunakan tampilan sebagai sebuah klien API akan terhubung setiap sekarang kemudian dan mengunduh buku untuk buku diterbitkan sejak kunjugan terakhir. Tetapi jika tidak ada buku baru muncul sejak itu, itu adalah pembuangan dari waktu CPU dan lebar pita untuk mengambil buku-buku dari basisdata, mengirim tanggapan penuh dan mengirim itu ke klien. Itu mungkin lebih baik untuk menanyakan API ketika kebanyakan buku saat ini telah diterbitkan.</p>\n<p>Kami memetakan UTL pada tampilan daftar buku di 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.urls</span> <span class=\"kn\">import</span> path\n<span class=\"kn\">from</span> <span class=\"nn\">books.views</span> <span class=\"kn\">import</span> BookListView\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    path<span class=\"p\">(</span><span class=\"s2\">&quot;books/&quot;</span><span class=\"p\">,</span> BookListView<span class=\"o\">.</span>as_view<span class=\"p\">()),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Dan tampilan:</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.http</span> <span class=\"kn\">import</span> HttpResponse\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> ListView\n<span class=\"kn\">from</span> <span class=\"nn\">books.models</span> <span class=\"kn\">import</span> Book\n\n\n<span class=\"k\">class</span> <span class=\"nc\">BookListView</span><span class=\"p\">(</span>ListView<span class=\"p\">):</span>\n    model <span class=\"o\">=</span> Book\n\n    <span class=\"k\">def</span> <span class=\"nf\">head</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        last_book <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_queryset<span class=\"p\">()</span><span class=\"o\">.</span>latest<span class=\"p\">(</span><span class=\"s2\">&quot;publication_date&quot;</span><span class=\"p\">)</span>\n        response <span class=\"o\">=</span> HttpResponse<span class=\"p\">(</span>\n            <span class=\"c1\"># RFC 1123 date format.</span>\n            headers<span class=\"o\">=</span><span class=\"p\">{</span>\n                <span class=\"s2\">&quot;Last-Modified&quot;</span><span class=\"p\">:</span> last_book<span class=\"o\">.</span>publication_date<span class=\"o\">.</span>strftime<span class=\"p\">(</span>\n                    <span class=\"s2\">&quot;</span><span class=\"si\">%a</span><span class=\"s2\">, </span><span class=\"si\">%d</span><span class=\"s2\"> %b %Y %H:%M:%S GMT&quot;</span>\n                <span class=\"p\">)</span>\n            <span class=\"p\">},</span>\n        <span class=\"p\">)</span>\n        <span class=\"k\">return</span> response\n</code></pre></div>\n<p>If the view is accessed from a <code class=\"docutils literal notranslate\">GET</code> request, an object list is returned in\nthe response (using the <code class=\"docutils literal notranslate\">book_list.html</code> template). But if the client issues\na <code class=\"docutils literal notranslate\">HEAD</code> request, the response has an empty body and the <code class=\"docutils literal notranslate\">Last-Modified</code>\nheader indicates when the most recent book was published. Based on this\ninformation, the client may or may not download the full object list.</p>\n</section>\n</section>\n<section id=\"asynchronous-class-based-views\">\n<span id=\"async-class-based-views\"></span><h2>Asynchronous class-based views<a class=\"heading-anchor\" href=\"#asynchronous-class-based-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>As well as the synchronous (<code class=\"docutils literal notranslate\">def</code>) method handlers already shown, <code class=\"docutils literal notranslate\">View</code>\nsubclasses may define asynchronous (<code class=\"docutils literal notranslate\">async def</code>) method handlers to leverage\nasynchronous code using <code class=\"docutils literal notranslate\">await</code>:</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\">import</span> <span class=\"nn\">asyncio</span>\n<span class=\"kn\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> HttpResponse\n<span class=\"kn\">from</span> <span class=\"nn\">django.views</span> <span class=\"kn\">import</span> View\n\n\n<span class=\"k\">class</span> <span class=\"nc\">AsyncView</span><span class=\"p\">(</span>View<span class=\"p\">):</span>\n    <span class=\"k\">async</span> <span class=\"k\">def</span> <span class=\"nf\">get</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> request<span class=\"p\">,</span> <span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        <span class=\"c1\"># Perform io-blocking view logic using await, sleep for example.</span>\n        <span class=\"k\">await</span> asyncio<span class=\"o\">.</span>sleep<span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n        <span class=\"k\">return</span> HttpResponse<span class=\"p\">(</span><span class=\"s2\">&quot;Hello async world!&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Within a single view-class, all user-defined method handlers must be either\nsynchronous, using <code class=\"docutils literal notranslate\">def</code>, or all asynchronous, using <code class=\"docutils literal notranslate\">async def</code>. An\n<code class=\"docutils literal notranslate\">ImproperlyConfigured</code> exception will be raised in <code class=\"docutils literal notranslate\">as_view()</code> if <code class=\"docutils literal notranslate\">def</code>\nand <code class=\"docutils literal notranslate\">async def</code> declarations are mixed.</p>\n<p>Django will automatically detect asynchronous views and run them in an\nasynchronous context. You can read more about Django's asynchronous support,\nand how to best use async views, in <a class=\"reference internal\" href=\"/id/6.1/topics/async/\"><span class=\"doc\">Dukungan asinkronus</span></a>.</p>\n</section>","rootId":"class-based-views","toc":[{"title":"Contoh dasar","anchor":"basic-examples","children":[]},{"title":"Penggunaan dalam URLconf anda","anchor":"usage-in-your-urlconf","children":[]},{"title":"Mensubkelaskan tampilan umum","anchor":"subclassing-generic-views","children":[{"title":"mendukung cara HTTP lain","anchor":"supporting-other-http-methods","children":[]}]},{"title":"Asynchronous class-based views","anchor":"asynchronous-class-based-views","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Menggunakan Django","url":"/id/6.1/topics/"}],"prev":{"docname":"topics/templates","title":"Cetakan","url":"/id/6.1/topics/templates/"},"next":{"docname":"topics/class-based-views/intro","title":"Perkenalan ke tampilan berdasarkan-kelas","url":"/id/6.1/topics/class-based-views/intro/"},"formats":{"html":"/id/6.1/topics/class-based-views/","markdown":"/id/6.1/topics/class-based-views.md","json":"/id/6.1/topics/class-based-views.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/topics/class-based-views/index.txt","official":"https://docs.djangoproject.com/id/6.1/topics/class-based-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","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}