{"title":"Menggunakan mixin dengan tampilan berdasarkan-kelas","version":"5.2","locale":"id","docname":"topics/class-based-views/mixins","url":"/id/5.2/topics/class-based-views/mixins/","canonical":"https://djangodocs.dev/id/5.2/topics/class-based-views/mixins/","summary":"Tampilan berdasarkan-kelas siap-pakai Django menyediakan banyak fungsionalitas, tetapi beberapa dari itu anda mungkin ingin menggunakannya terpisah.","html":"<h1>Menggunakan mixin dengan tampilan berdasarkan-kelas<a class=\"heading-anchor\" href=\"#using-mixins-with-class-based-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"admonition admonition-caution\" role=\"note\">\n<p class=\"admonition-title\">Hati-hati</p>\n<p>Ini adalah sebuah topik lanjut. Sebuah pengetahuan dari <a class=\"reference internal\" href=\"/id/5.2/topics/class-based-views/\"><span class=\"doc\">Django's class-based views</span></a> disarankan sebelum menjelajahi teknik-teknis ini.</p>\n</aside>\n<p>Tampilan berdasarkan-kelas siap-pakai Django menyediakan banyak fungsionalitas, tetapi beberapa dari itu anda mungkin ingin menggunakannya terpisah. Sebagai contoh, anda mungkin ingin menulis sebuah tampilan yang membangun cetakan untuk membuat tanggapan HTTP, tetapi anda tidak dapat menggunakan <a class=\"reference internal\" href=\"/id/5.2/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>; mungkin anda butuh membangun cetakan hanya pada <code class=\"docutils literal notranslate\">POST</code>, dengan <code class=\"docutils literal notranslate\">GET</code> melakukan sesuatu lain seluruhnya. Selagi anda dapat menggunakan <a class=\"reference internal\" href=\"/id/5.2/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> langsung, ini akan kemungkinan hasil di kode ganda.</p>\n<p>Untuk alasan ini, Django juga menyediakan sejumlah mixin yang menyediakan fungsionalitas diskrit lebih. Pembangunan cetakan, sebagai contoh, dienkapsulasi di <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin\" title=\"django.views.generic.base.TemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponseMixin</code></a>. Dokumentasi acuan Django mengandung <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins/\"><span class=\"doc\">full documentation of all the mixins</span></a>.</p>\n<section id=\"context-and-template-responses\">\n<h2>Tanggapan koteks dan cetakan<a class=\"heading-anchor\" href=\"#context-and-template-responses\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Dua mixin pusat disediakan yang membantu dalam antarmuka tetap untuk bekerja dengan cetakan di tampilan berdasarkan-kelas.</p>\n<dl>\n<dt><a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin\" title=\"django.views.generic.base.TemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponseMixin</code></a></dt><dd><p>Setiap tampilan siap pakai yang mengembalikan <a class=\"reference internal\" href=\"/id/5.2/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> akan memanggil metode <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.render_to_response\" title=\"django.views.generic.base.TemplateResponseMixin.render_to_response\"><code class=\"xref py py-meth docutils literal notranslate\">render_to_response()</code></a> yang <code class=\"docutils literal notranslate\">TemplateResponseMixin</code> sediakan. Kebanyakan dari waktu ini akan dipanggil untuk anda (sebagai contoh, itu dipanggil oleh metode <code class=\"docutils literal notranslate\">get()</code> diterapkan oleh kedua <a class=\"reference internal\" href=\"/id/5.2/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> and <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a>); demikian pula, itu tidak mungkin anda butuh menimpa itu, meskipun jika anda ingin tanggapan anda mengembalikan sesuatu tidak dibangun melalui cetakan Django kemudian anda akan ingin melakukan itu. Sebagai contoh dari ini, lihat <a class=\"reference internal\" href=\"#jsonresponsemixin-example\"><span class=\"std std-ref\">JSONResponseMixin example</span></a>.</p>\n<p><code class=\"docutils literal notranslate\">render_to_response()</code> itself calls\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.get_template_names\" title=\"django.views.generic.base.TemplateResponseMixin.get_template_names\"><code class=\"xref py py-meth docutils literal notranslate\">get_template_names()</code></a>,\nwhich by default will look up\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.template_name\" title=\"django.views.generic.base.TemplateResponseMixin.template_name\"><code class=\"xref py py-attr docutils literal notranslate\">template_name</code></a> on\nthe class-based view; two other mixins\n(<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectTemplateResponseMixin</code></a>\nand\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectTemplateResponseMixin\" title=\"django.views.generic.list.MultipleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectTemplateResponseMixin</code></a>)\noverride this to provide more flexible defaults when dealing with actual\nobjects.</p>\n</dd>\n<dt><a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.ContextMixin\" title=\"django.views.generic.base.ContextMixin\"><code class=\"xref py py-class docutils literal notranslate\">ContextMixin</code></a></dt><dd><p>Every built in view which needs context data, such as for rendering a\ntemplate (including <code class=\"docutils literal notranslate\">TemplateResponseMixin</code> above), should call\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.ContextMixin.get_context_data\" title=\"django.views.generic.base.ContextMixin.get_context_data\"><code class=\"xref py py-meth docutils literal notranslate\">get_context_data()</code></a> passing\nany data they want to ensure is in there as keyword arguments.\n<code class=\"docutils literal notranslate\">get_context_data()</code> returns a dictionary; in <code class=\"docutils literal notranslate\">ContextMixin</code> it\nreturns its keyword arguments, but it is common to override this to add\nmore members to the dictionary. You can also use the\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.ContextMixin.extra_context\" title=\"django.views.generic.base.ContextMixin.extra_context\"><code class=\"xref py py-attr docutils literal notranslate\">extra_context</code></a> attribute.</p>\n</dd>\n</dl>\n</section>\n<section id=\"building-up-django-s-generic-class-based-views\">\n<h2>Membangun tampilan berdasarkan-kelas umum Django<a class=\"heading-anchor\" href=\"#building-up-django-s-generic-class-based-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Mari kita lihat bagaimana tampilan berdasarkan-kelas umum Django adalah membangun dari mixin disediakan fungsionalitas diskrit. Kami akan mempertimbangkan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a>, yang membangun tampilan &quot;detail&quot; dari sebuah obyek, dan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.list.ListView\" title=\"django.views.generic.list.ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a>, yang akan membangun sebuah daftar dari obyek, khususnya dari queryset, dan pilihannya memberi nomor mereka. Ini akan memperkenalkan kita untuk empat mixin yang diantara mereka menyediakan fungsionalitas berguna dengan antara obyek Django tunggal, atau banyak obyek.</p>\n<p>Ada juga mixin terlibat di tampilan sunting umum (<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-editing/#django.views.generic.edit.FormView\" title=\"django.views.generic.edit.FormView\"><code class=\"xref py py-class docutils literal notranslate\">FormView</code></a>, dan tampilan model-tertentu <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-editing/#django.views.generic.edit.CreateView\" title=\"django.views.generic.edit.CreateView\"><code class=\"xref py py-class docutils literal notranslate\">CreateView</code></a>, <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-editing/#django.views.generic.edit.UpdateView\" title=\"django.views.generic.edit.UpdateView\"><code class=\"xref py py-class docutils literal notranslate\">UpdateView</code></a> dan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-editing/#django.views.generic.edit.DeleteView\" title=\"django.views.generic.edit.DeleteView\"><code class=\"xref py py-class docutils literal notranslate\">DeleteView</code></a>), dan di tampilan umum berdasarkan-tanggal. Ini dicakupi di <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins/\"><span class=\"doc\">mixin reference documentation</span></a>.</p>\n<section id=\"detailview-working-with-a-single-django-object\">\n<h3><code class=\"docutils literal notranslate\">DetailView</code>: bekerja dengan obyek tunggal Django<a class=\"heading-anchor\" href=\"#detailview-working-with-a-single-django-object\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Untuk menunjukkan rincian dari sebuah obyek, kami dasarnya butuh melakukan dua hal: kami butuh mencari obyek dan kemudian kami butuh membuat <a class=\"reference internal\" href=\"/id/5.2/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> dengan cetakan yang cocok, dan obyek itu sebagai konteks.</p>\n<p>Untuk mendapatkan obyek, <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> bergantung pada <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>, yang menyediakan sebuah metode <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_object\" title=\"django.views.generic.detail.SingleObjectMixin.get_object\"><code class=\"xref py py-meth docutils literal notranslate\">get_object()</code></a> yang mencari tahu berdasarkan obyek pada URL dari permintaan (itu sepertinya untuk argumen kata kunci <code class=\"docutils literal notranslate\">pk</code> dan <code class=\"docutils literal notranslate\">slug</code> seperti dinyatakan di URLConf, dan mencari obyek antara dari atribut <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.model\" title=\"django.views.generic.detail.SingleObjectMixin.model\"><code class=\"xref py py-attr docutils literal notranslate\">model</code></a> pada tampilan, atau atribut <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.queryset\" title=\"django.views.generic.detail.SingleObjectMixin.queryset\"><code class=\"xref py py-attr docutils literal notranslate\">queryset</code></a> jika itu disediakan). <code class=\"docutils literal notranslate\">SingleObjectMixin</code> juga menimpa <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.ContextMixin.get_context_data\" title=\"django.views.generic.base.ContextMixin.get_context_data\"><code class=\"xref py py-meth docutils literal notranslate\">get_context_data()</code></a>, yang digunakan terhadap semua tampilan siap-pakai Django untuk memasok data konteks untuk membangun cetakan.</p>\n<p>Untuk kemudian sebuah <a class=\"reference internal\" href=\"/id/5.2/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a>, <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> menggunakan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectTemplateResponseMixin</code></a>, yang memperpanjang <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin\" title=\"django.views.generic.base.TemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponseMixin</code></a>, menimpa <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.get_template_names\" title=\"django.views.generic.base.TemplateResponseMixin.get_template_names\"><code class=\"xref py py-meth docutils literal notranslate\">get_template_names()</code></a> seperti diobrolkan diatas. Itu sebenarnya menyediakan kumpulan cukup canggih pilihan, tetapi satu utama yang paling orang akan gunakan adalah <code class=\"docutils literal notranslate\">&lt;app_label&gt;/&lt;model_name&gt;_detail.html</code>. Bagian <code class=\"docutils literal notranslate\">_detail</code> dapat dirubah dengan mengatur <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix\"><code class=\"xref py py-attr docutils literal notranslate\">template_name_suffix</code></a> pada subkelas atau sesuatu lain. (Sebagai contoh,  <a class=\"reference internal\" href=\"/id/5.2/topics/class-based-views/generic-editing/\"><span class=\"doc\">generic edit views</span></a> menggunakan <code class=\"docutils literal notranslate\">_form</code> untuk membuat dan memperbaharui tampilan, dan <code class=\"docutils literal notranslate\">_confirm_delete</code> untuk tampilan menghapus.)</p>\n</section>\n<section id=\"listview-working-with-many-django-objects\">\n<h3><code class=\"docutils literal notranslate\">ListView</code>: bekerja dengan banyak obyek Django<a class=\"heading-anchor\" href=\"#listview-working-with-many-django-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Daftar dari obyek mengikuti kurang lebih pola sama: kami butuh sebuah (kemungkinan diberikan nomor) daftar obyek, khususnya sebuah <a class=\"reference internal\" href=\"/id/5.2/ref/models/querysets/#django.db.models.query.QuerySet\" title=\"django.db.models.query.QuerySet\"><code class=\"xref py py-class docutils literal notranslate\">QuerySet</code></a>, dan kemudian kami butuh membuat sebuah <a class=\"reference internal\" href=\"/id/5.2/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a> dengan cetakan cocok menggunakan daftar itu dari obyek.</p>\n<p>Untuk mendapatkan objek, <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.list.ListView\" title=\"django.views.generic.list.ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> menggunakan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin\" title=\"django.views.generic.list.MultipleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectMixin</code></a>, yang menyediakan <code class=\"xref py py-meth docutils literal notranslate\">get_queryset()</code> dan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin.paginate_queryset\" title=\"django.views.generic.list.MultipleObjectMixin.paginate_queryset\"><code class=\"xref py py-meth docutils literal notranslate\">paginate_queryset()</code></a>. Berbeda dengan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>, tidak perlu untuk mengunci bagian dari URL untuk mengetahui kumpulan permintaan yang akan digunakan, jadi awalan menggunakan <code class=\"xref py py-attr docutils literal notranslate\">queryset</code> atau <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin.model\" title=\"django.views.generic.list.MultipleObjectMixin.model\"><code class=\"xref py py-attr docutils literal notranslate\">model</code></a> pada kelas tampilan. Alasan umum untuk mengesampingkan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin.get_queryset\" title=\"django.views.generic.list.MultipleObjectMixin.get_queryset\"><code class=\"xref py py-meth docutils literal notranslate\">get_queryset()</code></a> di sini adalah untuk memvariasikan objek secara dinamis, seperti bergantung pada pengguna saat ini atau untuk mengecualikan kiriman di masa mendatang untuk blog.</p>\n<p><a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin\" title=\"django.views.generic.list.MultipleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectMixin</code></a> juga menimpa <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.ContextMixin.get_context_data\" title=\"django.views.generic.base.ContextMixin.get_context_data\"><code class=\"xref py py-meth docutils literal notranslate\">get_context_data()</code></a> untuk menyertakan variabel konteks sesuai untuk penomoran halaman (menyediakan contoh jika penomoran halaman ditiadakan). Itu bergantung pada 11object_list`` dilewatkan di sebuah argumen kata kunci, yang <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> mengatur untuk itu.</p>\n<p>Untuk membuat sebuah <a class=\"reference internal\" href=\"/id/5.2/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a>, <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> kemudian menggunakan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectTemplateResponseMixin\" title=\"django.views.generic.list.MultipleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectTemplateResponseMixin</code></a>; seperti <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectTemplateResponseMixin</code></a> diatas, ini menimpa <code class=\"docutils literal notranslate\">get_template_names()</code> untuk menyediakan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectTemplateResponseMixin\" title=\"django.views.generic.list.MultipleObjectTemplateResponseMixin\"><code class=\"xref py py-meth docutils literal notranslate\">a range of options</code></a>, yang paling digunakan-umum berwujud <code class=\"docutils literal notranslate\">&lt;app_label&gt;/&lt;model_name&gt;_list.html</code>, dengan bagian <code class=\"docutils literal notranslate\">_list</code> kembali diambil dari atribut <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectTemplateResponseMixin.template_name_suffix\" title=\"django.views.generic.list.MultipleObjectTemplateResponseMixin.template_name_suffix\"><code class=\"xref py py-attr docutils literal notranslate\">template_name_suffix</code></a>. (Tampilan umum berdasarkan tanggal menggunakan akhiran seperti <code class=\"docutils literal notranslate\">_archive</code>, <code class=\"docutils literal notranslate\">_archive_year</code> dan sebagainya untuk menggunakan cetakan berbeda untuk beragam tampilan daftar berdasarkan-tanggal khusus.)</p>\n</section>\n</section>\n<section id=\"using-django-s-class-based-view-mixins\">\n<h2>Menggunakan mixin tampilan berdasarkan-kelas Django<a class=\"heading-anchor\" href=\"#using-django-s-class-based-view-mixins\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Now we've seen how Django's generic class-based views use the provided mixins,\nlet's look at other ways we can combine them. We're still going to be combining\nthem with either built-in class-based views, or other generic class-based\nviews, but there are a range of rarer problems you can solve than are provided\nfor by Django out of the box.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Peringatan</p>\n<p>Tidak semua mixin dapat digunakan bersama-sama, dan tidak semua tampilan berdasarkan kelas umum dapat digunakan dengan semua mixin lain. Disini kami menghadirkan beberapa contoh yang melakukan pekerjaan; jika anda ingin membawa bersama-sama fungsionalitas lain kemudian anda akan harus mempertimbangkan interaksi diantara atribut dan metode yang tumpang tindih diantara kelas-kelas berbeda anda sedang gunakan, dan bagaimana <a class=\"reference external\" href=\"https://www.python.org/download/releases/2.3/mro/\">method resolution order</a> akan mempengaruhi versi mana dari metode akan dipanggil di urutan apa.</p>\n<p>Acuan dokumentasi untuk <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/\"><span class=\"doc\">class-based views</span></a> dan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins/\"><span class=\"doc\">class-based view mixins</span></a> Django akan membantu anda dalam memahami atribut dan metode mana mungkin menyebabkan pertentangan diantara kelas dan mixin berbeda.</p>\n<p>Jika ragu, itu sering lebih baik mundur dan dasarkan pekerjaan anda pada <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#View\" title=\"View\"><code class=\"xref py py-class docutils literal notranslate\">View</code></a> atau <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#TemplateView\" title=\"TemplateView\"><code class=\"xref py py-class docutils literal notranslate\">TemplateView</code></a>, mungkin dengan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> dan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin\" title=\"django.views.generic.list.MultipleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectMixin</code></a>. Meskipun anda akan mungkin mengakhiri menulis kode lebih, itu lebih mungkin jelas dapat dimengerti pada seseorang lain datang ke itu kemudian, dan dengan sedikit interaksi untuk khawatir tentang anda akan menyimpan beberapa pemikiran anda sendiri. (Tentu saja, anda dapat selalu mempelajari kedalam penerapan Django dari tampilan berdasarkan-kelas umum untuk ilham pada bagaimana memecahkan masalah.)</p>\n</aside>\n<section id=\"using-singleobjectmixin-with-view\">\n<h3>Menggunakan <code class=\"docutils literal notranslate\">SingleObjectMixin</code> dengan View<a class=\"heading-anchor\" href=\"#using-singleobjectmixin-with-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If we want to write a class-based view that responds only to <code class=\"docutils literal notranslate\">POST</code>, we'll\nsubclass <a class=\"reference internal\" href=\"/id/5.2/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> and write a <code class=\"docutils literal notranslate\">post()</code> method\nin the subclass. However if we want our processing to work on a particular\nobject, identified from the URL, we'll want the functionality provided by\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>.</p>\n<p>Kami akan mempertunjukkan ini dengan model <code class=\"docutils literal notranslate\">Author</code> kami gunakan di <a class=\"reference internal\" href=\"/id/5.2/topics/class-based-views/generic-display/\"><span class=\"doc\">generic class-based views introduction</span></a>.</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">views.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> HttpResponseForbidden<span class=\"p\">,</span> HttpResponseRedirect\n<span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> reverse\n<span class=\"kn\">from</span> <span class=\"nn\">django.views</span> <span class=\"kn\">import</span> View\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.detail</span> <span class=\"kn\">import</span> SingleObjectMixin\n<span class=\"kn\">from</span> <span class=\"nn\">books.models</span> <span class=\"kn\">import</span> Author\n\n\n<span class=\"k\">class</span> <span class=\"nc\">RecordInterestView</span><span class=\"p\">(</span>SingleObjectMixin<span class=\"p\">,</span> View<span class=\"p\">):</span>\n    <span class=\"sd\">&quot;&quot;&quot;Records the current user&#39;s interest in an author.&quot;&quot;&quot;</span>\n\n    model <span class=\"o\">=</span> Author\n\n    <span class=\"k\">def</span> <span class=\"nf\">post</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=\"k\">if</span> <span class=\"ow\">not</span> request<span class=\"o\">.</span>user<span class=\"o\">.</span>is_authenticated<span class=\"p\">:</span>\n            <span class=\"k\">return</span> HttpResponseForbidden<span class=\"p\">()</span>\n\n        <span class=\"c1\"># Look up the author we&#39;re interested in.</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>object <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_object<span class=\"p\">()</span>\n        <span class=\"c1\"># Actually record interest somehow here!</span>\n\n        <span class=\"k\">return</span> HttpResponseRedirect<span class=\"p\">(</span>\n            reverse<span class=\"p\">(</span><span class=\"s2\">&quot;author-detail&quot;</span><span class=\"p\">,</span> kwargs<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;pk&quot;</span><span class=\"p\">:</span> <span class=\"bp\">self</span><span class=\"o\">.</span>object<span class=\"o\">.</span>pk<span class=\"p\">})</span>\n        <span class=\"p\">)</span>\n</code></pre></figure>\n<p>In practice you'd probably want to record the interest in a key-value\nstore rather than in a relational database, so we've left that bit\nout. The only bit of the view that needs to worry about using\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> is where we want to\nlook up the author we're interested in, which it does with a call to\n<code class=\"docutils literal notranslate\">self.get_object()</code>. Everything else is taken care of for us by the mixin.</p>\n<p>Kami dapat menghubungkan kedalam URL kami dengan cukup mudah:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">urls.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python 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> RecordInterestView\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"c1\"># ...</span>\n    path<span class=\"p\">(</span>\n        <span class=\"s2\">&quot;author/&lt;int:pk&gt;/interest/&quot;</span><span class=\"p\">,</span>\n        RecordInterestView<span class=\"o\">.</span>as_view<span class=\"p\">(),</span>\n        name<span class=\"o\">=</span><span class=\"s2\">&quot;author-interest&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>Catat kelompok bernama <code class=\"docutils literal notranslate\">pk</code>, yang <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_object\" title=\"django.views.generic.detail.SingleObjectMixin.get_object\"><code class=\"xref py py-meth docutils literal notranslate\">get_object()</code></a> menggunakan untuk mencari instance <code class=\"docutils literal notranslate\">Author</code>. Anda dapat juga menggunakan sebuah keong, atau apapun dari fitur-fitur lain dari <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>.</p>\n</section>\n<section id=\"using-singleobjectmixin-with-listview\">\n<h3>Menggunakan <code class=\"docutils literal notranslate\">SingleObjectMixin</code> dengan <code class=\"docutils literal notranslate\">ListView</code><a class=\"heading-anchor\" href=\"#using-singleobjectmixin-with-listview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.list.ListView\" title=\"django.views.generic.list.ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> menyediakan penomoran siap-pakai, tetapi anda mungkin ingin memberi nomor daftar dari obyek yang semua terkait (oleh foreign key) ke obyek lain. Di contoh penerbitan kami, anda mungkin ingin memberi nomor melalui semua buku-buku oleh penerbit tertentu.</p>\n<p>Satu cara melakukan ini adalah memadukan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> dengan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>, sehingga queryset untuk memberi nomor halaman dari buku dapat  menggantung penerbit ditemukan sebagai obyek tunggal. Untuk melakukan ini, kami butuh memiliki dua queryset berbeda:</p>\n<dl class=\"simple\">\n<dt>Queryset <code class=\"docutils literal notranslate\">Book</code> untuk digunakan oleh <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.list.ListView\" title=\"django.views.generic.list.ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a></dt><dd><p>Since we have access to the <code class=\"docutils literal notranslate\">Publisher</code> whose books we want to list, we\noverride <code class=\"docutils literal notranslate\">get_queryset()</code> and use the <code class=\"docutils literal notranslate\">Publisher</code>’s <a class=\"reference internal\" href=\"/id/5.2/topics/db/queries/#backwards-related-objects\"><span class=\"std std-ref\">reverse\nforeign key manager</span></a>.</p>\n</dd>\n<dt>Queryset <code class=\"docutils literal notranslate\">Publisher</code> untuk digunakan di <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_object\" title=\"django.views.generic.detail.SingleObjectMixin.get_object\"><code class=\"xref py py-meth docutils literal notranslate\">get_object()</code></a></dt><dd><p>Kami akan bergantung pada penerapan awalan dari <code class=\"docutils literal notranslate\">get_object()</code> untuk mengambil obyek <code class=\"docutils literal notranslate\">Publisher</code> benar. Bagaimanapun, kami butuh jelas melewatkan sebuah argumen <code class=\"docutils literal notranslate\">queryset</code> karena jika tidak penerapan awalan dari <code class=\"docutils literal notranslate\">get_object()</code> akan memanggil <code class=\"docutils literal notranslate\">get_queryset()</code> yang kami telah timpa untuk mengembalikan obyek <code class=\"docutils literal notranslate\">Book</code> daripada <code class=\"docutils literal notranslate\">Publisher</code>.</p>\n</dd>\n</dl>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Catatan</p>\n<p>Kami harus berpikir hati-hati tentang <code class=\"docutils literal notranslate\">get_context_data()</code>. Sejak kedua <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> dan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> akan menaruh hal-hal dalam data konteks dibawah nilai dari <code class=\"docutils literal notranslate\">context_object_name</code> jika itu disetel, kami akan jelas memastikan <code class=\"docutils literal notranslate\">Publisher</code> adalah dalam data konteks. <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> akan menambah dalam <code class=\"docutils literal notranslate\">page_obj</code> dan <code class=\"docutils literal notranslate\">paginator</code> cocok untuk kami menyediakan kami ingat memanggil <code class=\"docutils literal notranslate\">super()</code>.</p>\n</aside>\n<p>Sekarang kami dapat menulis <code class=\"docutils literal notranslate\">PublisherDetailView</code> baru:</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.views.generic</span> <span class=\"kn\">import</span> ListView\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.detail</span> <span class=\"kn\">import</span> SingleObjectMixin\n<span class=\"kn\">from</span> <span class=\"nn\">books.models</span> <span class=\"kn\">import</span> Publisher\n\n\n<span class=\"k\">class</span> <span class=\"nc\">PublisherDetailView</span><span class=\"p\">(</span>SingleObjectMixin<span class=\"p\">,</span> ListView<span class=\"p\">):</span>\n    paginate_by <span class=\"o\">=</span> <span class=\"mi\">2</span>\n    template_name <span class=\"o\">=</span> <span class=\"s2\">&quot;books/publisher_detail.html&quot;</span>\n\n    <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=\"bp\">self</span><span class=\"o\">.</span>object <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_object<span class=\"p\">(</span>queryset<span class=\"o\">=</span>Publisher<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">())</span>\n        <span class=\"k\">return</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>get<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\n    <span class=\"k\">def</span> <span class=\"nf\">get_context_data</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        context <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>get_context_data<span class=\"p\">(</span><span class=\"o\">**</span>kwargs<span class=\"p\">)</span>\n        context<span class=\"p\">[</span><span class=\"s2\">&quot;publisher&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>object\n        <span class=\"k\">return</span> context\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_queryset</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>object<span class=\"o\">.</span>book_set<span class=\"o\">.</span>all<span class=\"p\">()</span>\n</code></pre></div>\n<p>Perhatikan bagaimana kami menyetel <code class=\"docutils literal notranslate\">self.object</code> dalam <code class=\"docutils literal notranslate\">get()</code> jadi kami dapat menggunakan itu kembali kemudian dalam <code class=\"docutils literal notranslate\">get_context_data()</code> dan <code class=\"docutils literal notranslate\">get_queryset()</code>. Jika anda tidak menyetel <code class=\"docutils literal notranslate\">template_name</code>, cetakan akan awalan menjadi pilihan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> biasa, yang dalam kasus ini akan menjadi <code class=\"docutils literal notranslate\">&quot;books/book_list.html&quot;</code> karena itu adalah daftar dari buku-buku; <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#ListView\" title=\"ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a> tidak mengetahui apapun tentang <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a>, jadi itu tidak mempunyai petunjuk tampilan ini adalah hubungannya dengan <code class=\"docutils literal notranslate\">Publisher</code>.</p>\n<p><code class=\"docutils literal notranslate\">paginate_by</code> sengaja kecil dalam contoh sehingga anda tidak perlu membuat banyak buku untuk melihat penomoran halaman bekerja! Ini adalah cetakan anda ingin gunakan:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">extends</span> <span class=\"s2\">&quot;base.html&quot;</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">block</span> <span class=\"nv\">content</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span>Publisher <span class=\"cp\">{{</span> <span class=\"nv\">publisher.name</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">h2</span><span class=\"p\">&gt;</span>\n\n    <span class=\"p\">&lt;</span><span class=\"nt\">ol</span><span class=\"p\">&gt;</span>\n      <span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">book</span> <span class=\"k\">in</span> <span class=\"nv\">page_obj</span> <span class=\"cp\">%}</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">book.title</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">li</span><span class=\"p\">&gt;</span>\n      <span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">ol</span><span class=\"p\">&gt;</span>\n\n    <span class=\"p\">&lt;</span><span class=\"nt\">div</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;pagination&quot;</span><span class=\"p\">&gt;</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">span</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;step-links&quot;</span><span class=\"p\">&gt;</span>\n            <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">page_obj.has_previous</span> <span class=\"cp\">%}</span>\n                <span class=\"p\">&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;?page=</span><span class=\"cp\">{{</span> <span class=\"nv\">page_obj.previous_page_number</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>previous<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;</span>\n            <span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n\n            <span class=\"p\">&lt;</span><span class=\"nt\">span</span> <span class=\"na\">class</span><span class=\"o\">=</span><span class=\"s\">&quot;current&quot;</span><span class=\"p\">&gt;</span>\n                Page <span class=\"cp\">{{</span> <span class=\"nv\">page_obj.number</span> <span class=\"cp\">}}</span> of <span class=\"cp\">{{</span> <span class=\"nv\">paginator.num_pages</span> <span class=\"cp\">}}</span>.\n            <span class=\"p\">&lt;/</span><span class=\"nt\">span</span><span class=\"p\">&gt;</span>\n\n            <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">page_obj.has_next</span> <span class=\"cp\">%}</span>\n                <span class=\"p\">&lt;</span><span class=\"nt\">a</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;?page=</span><span class=\"cp\">{{</span> <span class=\"nv\">page_obj.next_page_number</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>next<span class=\"p\">&lt;/</span><span class=\"nt\">a</span><span class=\"p\">&gt;</span>\n            <span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n        <span class=\"p\">&lt;/</span><span class=\"nt\">span</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">div</span><span class=\"p\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endblock</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"avoid-anything-more-complex\">\n<h2>Hindari apapun lebih rumit<a class=\"heading-anchor\" href=\"#avoid-anything-more-complex\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Umumnya anda dapat gunakan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin\" title=\"django.views.generic.base.TemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponseMixin</code></a> dan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> ketika anda butuh fungsionalitas mereka. Seperti ditunjukkan diatas, dengan sedikit perawatan anda dapat bahkan memadukan <code class=\"docutils literal notranslate\">SingleObjectMixin</code> dengan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.list.ListView\" title=\"django.views.generic.list.ListView\"><code class=\"xref py py-class docutils literal notranslate\">ListView</code></a>. Bagaimanapun hal-hal meningkat semakin rumit ketika anda mencoba melakukannya, dan aturan bagus dari ibu jari adalah:</p>\n<aside class=\"admonition admonition-hint\" role=\"note\">\n<p class=\"admonition-title\">Petunjuk</p>\n<p>Setiap tampilan anda harus menggunakan hanya mixin atau tampilan dari saru dari kelompok dari tampilan berdasarkan-kelas umum: <a class=\"reference internal\" href=\"/id/5.2/topics/class-based-views/generic-display/\"><span class=\"doc\">detail, list</span></a>, <a class=\"reference internal\" href=\"/id/5.2/topics/class-based-views/generic-editing/\"><span class=\"doc\">editing</span></a> dan tanggal. Sebagai contoh itu adalah baik memadukan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#TemplateView\" title=\"TemplateView\"><code class=\"xref py py-class docutils literal notranslate\">TemplateView</code></a> (tampilan siap pakai) dengan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin\" title=\"django.views.generic.list.MultipleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">MultipleObjectMixin</code></a> (daftar umum), tetapi anda mungkin memiliki masalah memadukan <code class=\"docutils literal notranslate\">SingleObjectMixin</code> (rincian umum) dengan <code class=\"docutils literal notranslate\">MultipleObjectMixin</code> (daftar umum).</p>\n</aside>\n<p>Untuk menunjukkan apa yang terjadi ketika anda mencoba mendapatkan lebih mutakhir, kami menunjukkan sebuah contoh yang mengorbankan kesiapan dan rawatan ketika ada pemecahan termudah. Pertama, mari kita lihat usaha naif untuk memadukan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> dengan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin\" title=\"django.views.generic.edit.FormMixin\"><code class=\"xref py py-class docutils literal notranslate\">FormMixin</code></a> untuk mengadakan kami pada <code class=\"docutils literal notranslate\">POST</code> sebuah <a class=\"reference internal\" href=\"/id/5.2/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\">Form</code></a> Django ke URL sama ketika kami sedang memperlihatkan sebuah obyek menggunakan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a>.</p>\n<section id=\"using-formmixin-with-detailview\">\n<h3>Menggunakan <code class=\"docutils literal notranslate\">FormMixin</code> dengan <code class=\"docutils literal notranslate\">DetailView</code><a class=\"heading-anchor\" href=\"#using-formmixin-with-detailview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Berpikir kembali ke contoh paling awal dari menggunakan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#View\" title=\"View\"><code class=\"xref py py-class docutils literal notranslate\">View</code></a> dan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> bersama-sama. Kami sedang merekam sebuah minat pengguna dalam penulis tertentu; katakan sekarang yang kami ingin membiarkan mereka meninggalkan pesan mengatakan mengapa mereka menyukainya. Kembali, mari kita anggap tidak menyimpan ini dalam basisdata hubungan tetapi sebagai gantinya di lebih esoterik yang kami tidak akan khawatir disini.</p>\n<p>Pada titik ini itu adalah alamiah mencapai untuk <a class=\"reference internal\" href=\"/id/5.2/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\">Form</code></a> untuk mengenkapsulasi informasi dikirim dari peramban pengguna pada Django. Katakan juga bahwa kami melakukan investasi di <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Representational_state_transfer\">REST</a>, jadi kami ingin menggunakan URL sama untuk memperlihatkan penulis sebagai untuk menangkap pesan dar pengguna. Mari kita menulis kembali <code class=\"docutils literal notranslate\">AuthorDetailView</code> kami untuk melakukan itu.</p>\n<p>Kami akan menjaga penanganan <code class=\"docutils literal notranslate\">GET</code> dari <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a>, meskipun kami akan harus menambah <a class=\"reference internal\" href=\"/id/5.2/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\">Form</code></a> kedalam data konteks jadi kami dapat mengirim itu dalam cetakan. Kami akan juga ingin menarik dalam pengolahan formulir <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin\" title=\"django.views.generic.edit.FormMixin\"><code class=\"xref py py-class docutils literal notranslate\">FormMixin</code></a>, dan menulis sedikit kode sehingga pada <code class=\"docutils literal notranslate\">POST</code> formulir mendapatkan panggilan yang sesuai.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Catatan</p>\n<p>Kami menggunakan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin\" title=\"django.views.generic.edit.FormMixin\"><code class=\"xref py py-class docutils literal notranslate\">FormMixin</code></a> dan menerapkan <code class=\"docutils literal notranslate\">post()</code> kami sendiri daripada mencoba mencampur <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> dengan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#FormView\" title=\"FormView\"><code class=\"xref py py-class docutils literal notranslate\">FormView</code></a> (yang menyediakan sudah <code class=\"docutils literal notranslate\">post()</code> cocok) karena kedua dari tampilan menerapkan <code class=\"docutils literal notranslate\">get()</code>, dan hal-hal akan menjadi lebih membingungkan.</p>\n</aside>\n<p><code class=\"docutils literal notranslate\">AuthorDetailView</code> kami yang baru terlihat seperti ini:</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\"># CAUTION: you almost certainly do not want to do this.</span>\n<span class=\"c1\"># It is provided as part of a discussion of problems you can</span>\n<span class=\"c1\"># run into when combining different generic class-based view</span>\n<span class=\"c1\"># functionality that is not designed to be used together.</span>\n\n<span class=\"kn\">from</span> <span class=\"nn\">django</span> <span class=\"kn\">import</span> forms\n<span class=\"kn\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> HttpResponseForbidden\n<span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> reverse\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> DetailView\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.edit</span> <span class=\"kn\">import</span> FormMixin\n<span class=\"kn\">from</span> <span class=\"nn\">books.models</span> <span class=\"kn\">import</span> Author\n\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorInterestForm</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>Form<span class=\"p\">):</span>\n    message <span class=\"o\">=</span> forms<span class=\"o\">.</span>CharField<span class=\"p\">()</span>\n\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorDetailView</span><span class=\"p\">(</span>FormMixin<span class=\"p\">,</span> DetailView<span class=\"p\">):</span>\n    model <span class=\"o\">=</span> Author\n    form_class <span class=\"o\">=</span> AuthorInterestForm\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_success_url</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> reverse<span class=\"p\">(</span><span class=\"s2\">&quot;author-detail&quot;</span><span class=\"p\">,</span> kwargs<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;pk&quot;</span><span class=\"p\">:</span> <span class=\"bp\">self</span><span class=\"o\">.</span>object<span class=\"o\">.</span>pk<span class=\"p\">})</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">post</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=\"k\">if</span> <span class=\"ow\">not</span> request<span class=\"o\">.</span>user<span class=\"o\">.</span>is_authenticated<span class=\"p\">:</span>\n            <span class=\"k\">return</span> HttpResponseForbidden<span class=\"p\">()</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>object <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_object<span class=\"p\">()</span>\n        form <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_form<span class=\"p\">()</span>\n        <span class=\"k\">if</span> form<span class=\"o\">.</span>is_valid<span class=\"p\">():</span>\n            <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>form_valid<span class=\"p\">(</span>form<span class=\"p\">)</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>form_invalid<span class=\"p\">(</span>form<span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">form_valid</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> form<span class=\"p\">):</span>\n        <span class=\"c1\"># Here, we would record the user&#39;s interest using the message</span>\n        <span class=\"c1\"># passed in form.cleaned_data[&#39;message&#39;]</span>\n        <span class=\"k\">return</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>form_valid<span class=\"p\">(</span>form<span class=\"p\">)</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">get_success_url()</code> provides somewhere to redirect to, which gets used\nin the default implementation of <code class=\"docutils literal notranslate\">form_valid()</code>. We have to provide our\nown <code class=\"docutils literal notranslate\">post()</code> as noted earlier.</p>\n</section>\n<section id=\"a-better-solution\">\n<h3>Pemecahan terbaik<a class=\"heading-anchor\" href=\"#a-better-solution\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The number of subtle interactions between\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin\" title=\"django.views.generic.edit.FormMixin\"><code class=\"xref py py-class docutils literal notranslate\">FormMixin</code></a> and <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> is\nalready testing our ability to manage things. It's unlikely you'd want to\nwrite this kind of class yourself.</p>\n<p>In this case, you could write the <code class=\"docutils literal notranslate\">post()</code> method yourself, keeping\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> as the only generic functionality, although writing\n<a class=\"reference internal\" href=\"/id/5.2/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\">Form</code></a> handling code involves a lot of duplication.</p>\n<p>Alternatively, it would still be less work than the above approach to\nhave a separate view for processing the form, which could use\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-editing/#django.views.generic.edit.FormView\" title=\"django.views.generic.edit.FormView\"><code class=\"xref py py-class docutils literal notranslate\">FormView</code></a> distinct from\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> without concerns.</p>\n</section>\n<section id=\"an-alternative-better-solution\">\n<h3>Sebuah pilihan lain pemecahan terbaik<a class=\"heading-anchor\" href=\"#an-alternative-better-solution\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Apa kami sedang coba lakukan disini adalah menggunakan dua kelas bebeda berdasarkan tampilan dari URL sama. Jadi mengapa tidak lakukan hal itu? Kami mempunyai pembagian sangat jelas disini: permintaan <code class=\"docutils literal notranslate\">GET</code> harus mendapatkan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#DetailView\" title=\"DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> (dengan <a class=\"reference internal\" href=\"/id/5.2/ref/forms/api/#django.forms.Form\" title=\"django.forms.Form\"><code class=\"xref py py-class docutils literal notranslate\">Form</code></a> ditambahkan ke data konteks), dan permintaan <code class=\"docutils literal notranslate\">POST</code> harus mendapatkan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#FormView\" title=\"FormView\"><code class=\"xref py py-class docutils literal notranslate\">FormView</code></a>. Mari kita menyetek tampilan tersebut dahulu.</p>\n<p>The <code class=\"docutils literal notranslate\">AuthorDetailView</code> view is almost the same as <a class=\"reference internal\" href=\"/id/5.2/topics/class-based-views/generic-display/#generic-views-extra-work\"><span class=\"std std-ref\">when we\nfirst introduced AuthorDetailView</span></a>; we have to\nwrite our own <code class=\"docutils literal notranslate\">get_context_data()</code> to make the\n<code class=\"docutils literal notranslate\">AuthorInterestForm</code> available to the template. We'll skip the\n<code class=\"docutils literal notranslate\">get_object()</code> override from before for clarity:</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</span> <span class=\"kn\">import</span> forms\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> DetailView\n<span class=\"kn\">from</span> <span class=\"nn\">books.models</span> <span class=\"kn\">import</span> Author\n\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorInterestForm</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>Form<span class=\"p\">):</span>\n    message <span class=\"o\">=</span> forms<span class=\"o\">.</span>CharField<span class=\"p\">()</span>\n\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorDetailView</span><span class=\"p\">(</span>DetailView<span class=\"p\">):</span>\n    model <span class=\"o\">=</span> Author\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_context_data</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        context <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>get_context_data<span class=\"p\">(</span><span class=\"o\">**</span>kwargs<span class=\"p\">)</span>\n        context<span class=\"p\">[</span><span class=\"s2\">&quot;form&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> AuthorInterestForm<span class=\"p\">()</span>\n        <span class=\"k\">return</span> context\n</code></pre></div>\n<p>Then the <code class=\"docutils literal notranslate\">AuthorInterestFormView</code> is a <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#FormView\" title=\"FormView\"><code class=\"xref py py-class docutils literal notranslate\">FormView</code></a>, but we have to\nbring in <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin\" title=\"django.views.generic.detail.SingleObjectMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectMixin</code></a> so we can find\nthe author we're talking about, and we have to remember to set\n<code class=\"docutils literal notranslate\">template_name</code> to ensure that form errors will render the same template as\n<code class=\"docutils literal notranslate\">AuthorDetailView</code> is using on <code class=\"docutils literal notranslate\">GET</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\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> HttpResponseForbidden\n<span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> reverse\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic</span> <span class=\"kn\">import</span> FormView\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.detail</span> <span class=\"kn\">import</span> SingleObjectMixin\n\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorInterestFormView</span><span class=\"p\">(</span>SingleObjectMixin<span class=\"p\">,</span> FormView<span class=\"p\">):</span>\n    template_name <span class=\"o\">=</span> <span class=\"s2\">&quot;books/author_detail.html&quot;</span>\n    form_class <span class=\"o\">=</span> AuthorInterestForm\n    model <span class=\"o\">=</span> Author\n\n    <span class=\"k\">def</span> <span class=\"nf\">post</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=\"k\">if</span> <span class=\"ow\">not</span> request<span class=\"o\">.</span>user<span class=\"o\">.</span>is_authenticated<span class=\"p\">:</span>\n            <span class=\"k\">return</span> HttpResponseForbidden<span class=\"p\">()</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span>object <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>get_object<span class=\"p\">()</span>\n        <span class=\"k\">return</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>post<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\n    <span class=\"k\">def</span> <span class=\"nf\">get_success_url</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> reverse<span class=\"p\">(</span><span class=\"s2\">&quot;author-detail&quot;</span><span class=\"p\">,</span> kwargs<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;pk&quot;</span><span class=\"p\">:</span> <span class=\"bp\">self</span><span class=\"o\">.</span>object<span class=\"o\">.</span>pk<span class=\"p\">})</span>\n</code></pre></div>\n<p>Finally we bring this together in a new <code class=\"docutils literal notranslate\">AuthorView</code> view. We\nalready know that calling <a class=\"reference internal\" href=\"/id/5.2/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> on\na class-based view gives us something that behaves exactly like a function\nbased view, so we can do that at the point we choose between the two subviews.</p>\n<p>You can pass through keyword arguments to\n<a class=\"reference internal\" href=\"/id/5.2/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> in the same way you\nwould in your URLconf, such as if you wanted the <code class=\"docutils literal notranslate\">AuthorInterestFormView</code>\nbehavior to also appear at another URL but using a different template:</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.views</span> <span class=\"kn\">import</span> View\n\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorView</span><span class=\"p\">(</span>View<span class=\"p\">):</span>\n    <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        view <span class=\"o\">=</span> AuthorDetailView<span class=\"o\">.</span>as_view<span class=\"p\">()</span>\n        <span class=\"k\">return</span> view<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\n    <span class=\"k\">def</span> <span class=\"nf\">post</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        view <span class=\"o\">=</span> AuthorInterestFormView<span class=\"o\">.</span>as_view<span class=\"p\">()</span>\n        <span class=\"k\">return</span> view<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</code></pre></div>\n<p>Pendekatan ini dapat juga digunakan dengan tampilan berdasarkan-kelas umum lainnya diwarisi langsung dari <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#View\" title=\"View\"><code class=\"xref py py-class docutils literal notranslate\">View</code></a> or <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/flattened-index/#TemplateView\" title=\"TemplateView\"><code class=\"xref py py-class docutils literal notranslate\">TemplateView</code></a>, ketika itu menjaga tampilan berbeda sebagai terpisah mungkin.</p>\n</section>\n</section>\n<section id=\"more-than-just-html\">\n<span id=\"jsonresponsemixin-example\"></span><h2>Lebih dari hanya HTML<a class=\"heading-anchor\" href=\"#more-than-just-html\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Dimana bersinar tampilan berdasarkan-kelas adalah ketika anda ingin melakukan hal sama sebanyak kali. Kiranya anda sedang menulis sebuah API, dan setiap tampilan harus mengembalikan JSON daripada membangun HTML.</p>\n<p>Kami dapat membuat sebuah kelas mixin untuk digunakan di semua dari tampilan kami, menangani perubahan pada JSON sekali.</p>\n<p>Sebagai contoh, mixin JSON mungkin terlihat seperti ini:</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> JsonResponse\n\n\n<span class=\"k\">class</span> <span class=\"nc\">JSONResponseMixin</span><span class=\"p\">:</span>\n    <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    A mixin that can be used to render a JSON response.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">render_to_json_response</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> context<span class=\"p\">,</span> <span class=\"o\">**</span>response_kwargs<span class=\"p\">):</span>\n        <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Returns a JSON response, transforming &#39;context&#39; to make the payload.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> JsonResponse<span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span>get_data<span class=\"p\">(</span>context<span class=\"p\">),</span> <span class=\"o\">**</span>response_kwargs<span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_data</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> context<span class=\"p\">):</span>\n        <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Returns an object that will be serialized as JSON by json.dumps().</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"c1\"># Note: This is *EXTREMELY* naive; in reality, you&#39;ll need</span>\n        <span class=\"c1\"># to do much more complex handling to ensure that arbitrary</span>\n        <span class=\"c1\"># objects -- such as Django model instances or querysets</span>\n        <span class=\"c1\"># -- can be serialized as JSON.</span>\n        <span class=\"k\">return</span> context\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Catatan</p>\n<p>Periksa dokumentasi <a class=\"reference internal\" href=\"/id/5.2/topics/serialization/\"><span class=\"doc\">Menserialkan obyek-obyek Django</span></a> untuk informasi lebih pada bagaimana dengan benar merubah model Django dan queryset menjadi JSON.</p>\n</aside>\n<p>This mixin provides a <code class=\"docutils literal notranslate\">render_to_json_response()</code> method with the same signature\nas <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.render_to_response\" title=\"django.views.generic.base.TemplateResponseMixin.render_to_response\"><code class=\"xref py py-func docutils literal notranslate\">render_to_response()</code></a>.\nTo use it, we need to mix it into a <code class=\"docutils literal notranslate\">TemplateView</code> for example, and override\n<code class=\"docutils literal notranslate\">render_to_response()</code> to call <code class=\"docutils literal notranslate\">render_to_json_response()</code> instead:</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.views.generic</span> <span class=\"kn\">import</span> TemplateView\n\n\n<span class=\"k\">class</span> <span class=\"nc\">JSONView</span><span class=\"p\">(</span>JSONResponseMixin<span class=\"p\">,</span> TemplateView<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">render_to_response</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> context<span class=\"p\">,</span> <span class=\"o\">**</span>response_kwargs<span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>render_to_json_response<span class=\"p\">(</span>context<span class=\"p\">,</span> <span class=\"o\">**</span>response_kwargs<span class=\"p\">)</span>\n</code></pre></div>\n<p>Equally we could use our mixin with one of the generic views. We can make our\nown version of <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> by mixing\n<code class=\"docutils literal notranslate\">JSONResponseMixin</code> with the\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.detail.BaseDetailView\" title=\"django.views.generic.detail.BaseDetailView\"><code class=\"xref py py-class docutils literal notranslate\">BaseDetailView</code></a> -- (the\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> before template\nrendering behavior has been mixed in):</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.views.generic.detail</span> <span class=\"kn\">import</span> BaseDetailView\n\n\n<span class=\"k\">class</span> <span class=\"nc\">JSONDetailView</span><span class=\"p\">(</span>JSONResponseMixin<span class=\"p\">,</span> BaseDetailView<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">render_to_response</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> context<span class=\"p\">,</span> <span class=\"o\">**</span>response_kwargs<span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>render_to_json_response<span class=\"p\">(</span>context<span class=\"p\">,</span> <span class=\"o\">**</span>response_kwargs<span class=\"p\">)</span>\n</code></pre></div>\n<p>Tampilan ini dapat kemudian disebarkan dalam cara sama seperti lainnya <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a>, dengan persi perilaku sama -- kecuali untuk bentuk dari tanggapan.</p>\n<p>If you want to be really adventurous, you could even mix a\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/generic-display/#django.views.generic.detail.DetailView\" title=\"django.views.generic.detail.DetailView\"><code class=\"xref py py-class docutils literal notranslate\">DetailView</code></a> subclass that is able\nto return <em>both</em> HTML and JSON content, depending on some property of\nthe HTTP request, such as a query argument or an HTTP header. Mix in both the\n<code class=\"docutils literal notranslate\">JSONResponseMixin</code> and a\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectTemplateResponseMixin</code></a>,\nand override the implementation of\n<a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.render_to_response\" title=\"django.views.generic.base.TemplateResponseMixin.render_to_response\"><code class=\"xref py py-func docutils literal notranslate\">render_to_response()</code></a>\nto defer to the appropriate rendering method depending on the type of response\nthat the user requested:</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.views.generic.detail</span> <span class=\"kn\">import</span> SingleObjectTemplateResponseMixin\n\n\n<span class=\"k\">class</span> <span class=\"nc\">HybridDetailView</span><span class=\"p\">(</span>\n    JSONResponseMixin<span class=\"p\">,</span> SingleObjectTemplateResponseMixin<span class=\"p\">,</span> BaseDetailView\n<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"nf\">render_to_response</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> context<span class=\"p\">):</span>\n        <span class=\"c1\"># Look for a &#39;format=json&#39; GET argument</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span>request<span class=\"o\">.</span>GET<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;format&quot;</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"s2\">&quot;json&quot;</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>render_to_json_response<span class=\"p\">(</span>context<span class=\"p\">)</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>render_to_response<span class=\"p\">(</span>context<span class=\"p\">)</span>\n</code></pre></div>\n<p>Karena cara bahwa Python menyelesaikan metode melebihi batas, panggilan pada <code class=\"docutils literal notranslate\">super().render_to_response(context)</code> mengakhiri panggilan penerapan <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.render_to_response\" title=\"django.views.generic.base.TemplateResponseMixin.render_to_response\"><code class=\"xref py py-meth docutils literal notranslate\">render_to_response()</code></a> dari <a class=\"reference internal\" href=\"/id/5.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin\" title=\"django.views.generic.base.TemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponseMixin</code></a>.</p>\n</section>","rootId":"using-mixins-with-class-based-views","toc":[{"title":"Tanggapan koteks dan cetakan","anchor":"context-and-template-responses","children":[]},{"title":"Membangun tampilan berdasarkan-kelas umum Django","anchor":"building-up-django-s-generic-class-based-views","children":[{"title":"DetailView: bekerja dengan obyek tunggal Django","anchor":"detailview-working-with-a-single-django-object","children":[]},{"title":"ListView: bekerja dengan banyak obyek Django","anchor":"listview-working-with-many-django-objects","children":[]}]},{"title":"Menggunakan mixin tampilan berdasarkan-kelas Django","anchor":"using-django-s-class-based-view-mixins","children":[{"title":"Menggunakan SingleObjectMixin dengan View","anchor":"using-singleobjectmixin-with-view","children":[]},{"title":"Menggunakan SingleObjectMixin dengan ListView","anchor":"using-singleobjectmixin-with-listview","children":[]}]},{"title":"Hindari apapun lebih rumit","anchor":"avoid-anything-more-complex","children":[{"title":"Menggunakan FormMixin dengan DetailView","anchor":"using-formmixin-with-detailview","children":[]},{"title":"Pemecahan terbaik","anchor":"a-better-solution","children":[]},{"title":"Sebuah pilihan lain pemecahan terbaik","anchor":"an-alternative-better-solution","children":[]}]},{"title":"Lebih dari hanya HTML","anchor":"more-than-just-html","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Menggunakan Django","url":"/id/5.2/topics/"},{"docname":"topics/class-based-views/index","title":"Tampilan berdasarkan-kelas","url":"/id/5.2/topics/class-based-views/"}],"prev":{"docname":"topics/class-based-views/generic-editing","title":"Menangani formulir dengan tampilan berdasarkan-kelas","url":"/id/5.2/topics/class-based-views/generic-editing/"},"next":{"docname":"topics/migrations","title":"Perpindahan","url":"/id/5.2/topics/migrations/"},"formats":{"html":"/id/5.2/topics/class-based-views/mixins/","markdown":"/id/5.2/topics/class-based-views/mixins.md","json":"/id/5.2/topics/class-based-views/mixins.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/topics/class-based-views/mixins.txt","official":"https://docs.djangoproject.com/id/5.2/topics/class-based-views/mixins/","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"]}