{"title":"The \"sites\" framework","version":"1.11","locale":"id","docname":"ref/contrib/sites","url":"/id/1.11/ref/contrib/sites/","canonical":"https://djangodocs.dev/id/1.11/ref/contrib/sites/","summary":"Django comes with an optional \"sites\" framework. It's a hook for associating objects and functionality to particular websites, and it's a holding place for…","html":"<span id=\"the-sites-framework\"></span><h1>The &quot;sites&quot; framework<a class=\"heading-anchor\" href=\"#module-django.contrib.sites\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django comes with an optional &quot;sites&quot; framework. It's a hook for associating\nobjects and functionality to particular websites, and it's a holding place for\nthe domain names and &quot;verbose&quot; names of your Django-powered sites.</p>\n<p>Use it if your single Django installation powers more than one site and you\nneed to differentiate between those sites in some way.</p>\n<p>The sites framework is mainly based on a simple model:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sites.models.Site\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">models.</span><span class=\"sig-name descname\">Site</span><a class=\"heading-anchor\" href=\"#django.contrib.sites.models.Site\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A model for storing the <code class=\"docutils literal notranslate\">domain</code> and <code class=\"docutils literal notranslate\">name</code> attributes of a website.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sites.models.Site.domain\">\n<span class=\"sig-name descname\">domain</span><a class=\"heading-anchor\" href=\"#django.contrib.sites.models.Site.domain\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The fully qualified domain name associated with the website.\nFor example, <code class=\"docutils literal notranslate\">www.example.com</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sites.models.Site.name\">\n<span class=\"sig-name descname\">name</span><a class=\"heading-anchor\" href=\"#django.contrib.sites.models.Site.name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A human-readable &quot;verbose&quot; name for the website.</p>\n</dd></dl>\n\n</dd></dl>\n\n<p>The <a class=\"reference internal\" href=\"/id/1.11/ref/settings/#std-setting-SITE_ID\"><code class=\"xref std std-setting docutils literal notranslate\">SITE_ID</code></a> setting specifies the database ID of the\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> object associated with that\nparticular settings file. If the setting is omitted, the\n<a class=\"reference internal\" href=\"#django.contrib.sites.shortcuts.get_current_site\" title=\"django.contrib.sites.shortcuts.get_current_site\"><code class=\"xref py py-func docutils literal notranslate\">get_current_site()</code></a> function will\ntry to get the current site by comparing the\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site.domain\" title=\"django.contrib.sites.models.Site.domain\"><code class=\"xref py py-attr docutils literal notranslate\">domain</code></a> with the host name from\nthe <a class=\"reference internal\" href=\"/id/1.11/ref/request-response/#django.http.HttpRequest.get_host\" title=\"django.http.HttpRequest.get_host\"><code class=\"xref py py-meth docutils literal notranslate\">request.get_host()</code></a> method.</p>\n<p>How you use this is up to you, but Django uses it in a couple of ways\nautomatically via simple conventions.</p>\n<section id=\"example-usage\">\n<h2>Example usage<a class=\"heading-anchor\" href=\"#example-usage\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Why would you use sites? It's best explained through examples.</p>\n<section id=\"associating-content-with-multiple-sites\">\n<h3>Associating content with multiple sites<a class=\"heading-anchor\" href=\"#associating-content-with-multiple-sites\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The Django-powered sites <a class=\"reference external\" href=\"http://www.ljworld.com/\">LJWorld.com</a> and <a class=\"reference external\" href=\"http://www.lawrence.com/\">Lawrence.com</a> are operated by the\nsame news organization -- the Lawrence Journal-World newspaper in Lawrence,\nKansas. LJWorld.com focuses on news, while Lawrence.com focuses on local\nentertainment. But sometimes editors want to publish an article on <em>both</em>\nsites.</p>\n<p>The naive way of solving the problem would be to require site producers to\npublish the same story twice: once for LJWorld.com and again for Lawrence.com.\nBut that's inefficient for site producers, and it's redundant to store\nmultiple copies of the same story in the database.</p>\n<p>The better solution is simple: Both sites use the same article database, and an\narticle is associated with one or more sites. In Django model terminology,\nthat's represented by a <a class=\"reference internal\" href=\"/id/1.11/ref/models/fields/#django.db.models.ManyToManyField\" title=\"django.db.models.ManyToManyField\"><code class=\"xref py py-class docutils literal notranslate\">ManyToManyField</code></a> in the\n<code class=\"docutils literal notranslate\">Article</code> model:</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.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sites.models</span> <span class=\"kn\">import</span> Site\n\n<span class=\"k\">class</span> <span class=\"nc\">Article</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    headline <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">200</span><span class=\"p\">)</span>\n    <span class=\"c1\"># ...</span>\n    sites <span class=\"o\">=</span> models<span class=\"o\">.</span>ManyToManyField<span class=\"p\">(</span>Site<span class=\"p\">)</span>\n</code></pre></div>\n<p>Ini menyelesaikan beberapa hal cukup baik:</p>\n<ul>\n<li><p>Itu membiarkan pembuat situs menyunting semua isi -- pada kedua situs -- dalam antarmuka tunggal (admin Django).</p></li>\n<li><p>Itu berarti cerita sams tidak harus menerbitkan kedua kali di basisdata; itu hanya mempunyai rekaman tunggal di basisdata.</p></li>\n<li><p>Itu membiarkan pengembang situs menggunakan kode tampilan Django sama untuk kedua situs. Kode tampilan yang memperlihatkan cerita yang diberikan cukup memeriksa untuk memastikan cerita diminta pada situs saat ini. Itu terlihat sesuatu 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.contrib.sites.shortcuts</span> <span class=\"kn\">import</span> get_current_site\n\n<span class=\"k\">def</span> <span class=\"nf\">article_detail</span><span class=\"p\">(</span>request<span class=\"p\">,</span> article_id<span class=\"p\">):</span>\n    <span class=\"k\">try</span><span class=\"p\">:</span>\n        a <span class=\"o\">=</span> Article<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"nb\">id</span><span class=\"o\">=</span>article_id<span class=\"p\">,</span> sites__id<span class=\"o\">=</span>get_current_site<span class=\"p\">(</span>request<span class=\"p\">)</span><span class=\"o\">.</span>id<span class=\"p\">)</span>\n    <span class=\"k\">except</span> Article<span class=\"o\">.</span>DoesNotExist<span class=\"p\">:</span>\n        <span class=\"k\">raise</span> Http404<span class=\"p\">(</span><span class=\"s2\">&quot;Article does not exist on this site&quot;</span><span class=\"p\">)</span>\n    <span class=\"c1\"># ...</span>\n</code></pre></div>\n</li>\n</ul>\n</section>\n<section id=\"associating-content-with-a-single-site\">\n<h3>Associating content with a single site<a class=\"heading-anchor\" href=\"#associating-content-with-a-single-site\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Similarly, you can associate a model to the\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a>\nmodel in a many-to-one relationship, using\n<a class=\"reference internal\" href=\"/id/1.11/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a>.</p>\n<p>For example, if an article is only allowed on a single site, you'd use a model\nlike this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sites.models</span> <span class=\"kn\">import</span> Site\n\n<span class=\"k\">class</span> <span class=\"nc\">Article</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    headline <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">200</span><span class=\"p\">)</span>\n    <span class=\"c1\"># ...</span>\n    site <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>Site<span class=\"p\">,</span> on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">)</span>\n</code></pre></div>\n<p>Ini mempunyai keuntungan sama seperti digambarkan di bagian terakhir.</p>\n</section>\n<section id=\"hooking-into-the-current-site-from-views\">\n<span id=\"hooking-into-current-site-from-views\"></span><h3>Mengaitkan kedalam situs saat ini dari tampilan<a class=\"heading-anchor\" href=\"#hooking-into-the-current-site-from-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Anda dapat menggunakan kerangka kerja situs di tampilan Django anda untuk melakukan hal-hal tertentu berdasarkan pada situs dimana tampilan sedang dipanggil. Sebagai contoh:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">if</span> settings<span class=\"o\">.</span>SITE_ID <span class=\"o\">==</span> <span class=\"mi\">3</span><span class=\"p\">:</span>\n        <span class=\"c1\"># Do something.</span>\n        <span class=\"k\">pass</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"c1\"># Do something else.</span>\n        <span class=\"k\">pass</span>\n</code></pre></div>\n<p>Of course, it's ugly to hard-code the site IDs like that. This sort of\nhard-coding is best for hackish fixes that you need done quickly. The\ncleaner way of accomplishing the same thing is to check the current site's\ndomain:</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.contrib.sites.shortcuts</span> <span class=\"kn\">import</span> get_current_site\n\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    current_site <span class=\"o\">=</span> get_current_site<span class=\"p\">(</span>request<span class=\"p\">)</span>\n    <span class=\"k\">if</span> current_site<span class=\"o\">.</span>domain <span class=\"o\">==</span> <span class=\"s1\">&#39;foo.com&#39;</span><span class=\"p\">:</span>\n        <span class=\"c1\"># Do something</span>\n        <span class=\"k\">pass</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"c1\"># Do something else.</span>\n        <span class=\"k\">pass</span>\n</code></pre></div>\n<p>This has also the advantage of checking if the sites framework is installed,\nand return a <a class=\"reference internal\" href=\"#django.contrib.sites.requests.RequestSite\" title=\"django.contrib.sites.requests.RequestSite\"><code class=\"xref py py-class docutils literal notranslate\">RequestSite</code></a> instance if\nit is not.</p>\n<p>Jika anda tidak mempunyai akses ke obyek diminta, anda dapat menggunakan metode <code class=\"docutils literal notranslate\">get_current()</code> dari pengelola model <a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a>. Anda harus kemudian memastikan bahwa berkas pengaturan anda mengandung pengaturan <a class=\"reference internal\" href=\"/id/1.11/ref/settings/#std-setting-SITE_ID\"><code class=\"xref std std-setting docutils literal notranslate\">SITE_ID</code></a>. Contoh ini adalah setara pada satu sebelumnya:</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.contrib.sites.models</span> <span class=\"kn\">import</span> Site\n\n<span class=\"k\">def</span> <span class=\"nf\">my_function_without_request</span><span class=\"p\">():</span>\n    current_site <span class=\"o\">=</span> Site<span class=\"o\">.</span>objects<span class=\"o\">.</span>get_current<span class=\"p\">()</span>\n    <span class=\"k\">if</span> current_site<span class=\"o\">.</span>domain <span class=\"o\">==</span> <span class=\"s1\">&#39;foo.com&#39;</span><span class=\"p\">:</span>\n        <span class=\"c1\"># Do something</span>\n        <span class=\"k\">pass</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"c1\"># Do something else.</span>\n        <span class=\"k\">pass</span>\n</code></pre></div>\n</section>\n<section id=\"getting-the-current-domain-for-display\">\n<h3>Mendapatkan ranah saat ini untuk ditampilkan<a class=\"heading-anchor\" href=\"#getting-the-current-domain-for-display\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>LJWorld.com and Lawrence.com both have email alert functionality, which lets\nreaders sign up to get notifications when news happens. It's pretty basic: A\nreader signs up on a Web form and immediately gets an email saying,\n&quot;Thanks for your subscription.&quot;</p>\n<p>It'd be inefficient and redundant to implement this sign up processing code\ntwice, so the sites use the same code behind the scenes. But the &quot;thank you for\nsigning up&quot; notice needs to be different for each site. By using\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a>\nobjects, we can abstract the &quot;thank you&quot; notice to use the values of the\ncurrent site's <a class=\"reference internal\" href=\"#django.contrib.sites.models.Site.name\" title=\"django.contrib.sites.models.Site.name\"><code class=\"xref py py-attr docutils literal notranslate\">name</code></a> and\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site.domain\" title=\"django.contrib.sites.models.Site.domain\"><code class=\"xref py py-attr docutils literal notranslate\">domain</code></a>.</p>\n<p>Disini adalah contoh dari apa tampilan penanganan formulir kelihatannya:</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.contrib.sites.shortcuts</span> <span class=\"kn\">import</span> get_current_site\n<span class=\"kn\">from</span> <span class=\"nn\">django.core.mail</span> <span class=\"kn\">import</span> send_mail\n\n<span class=\"k\">def</span> <span class=\"nf\">register_for_newsletter</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"c1\"># Check form values, etc., and subscribe the user.</span>\n    <span class=\"c1\"># ...</span>\n\n    current_site <span class=\"o\">=</span> get_current_site<span class=\"p\">(</span>request<span class=\"p\">)</span>\n    send_mail<span class=\"p\">(</span>\n        <span class=\"s1\">&#39;Thanks for subscribing to </span><span class=\"si\">%s</span><span class=\"s1\"> alerts&#39;</span> <span class=\"o\">%</span> current_site<span class=\"o\">.</span>name<span class=\"p\">,</span>\n        <span class=\"s1\">&#39;Thanks for your subscription. We appreciate it.</span><span class=\"se\">\\n\\n</span><span class=\"s1\">-The </span><span class=\"si\">%s</span><span class=\"s1\"> team.&#39;</span> <span class=\"o\">%</span> <span class=\"p\">(</span>\n            current_site<span class=\"o\">.</span>name<span class=\"p\">,</span>\n        <span class=\"p\">),</span>\n        <span class=\"s1\">&#39;editor@</span><span class=\"si\">%s</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> current_site<span class=\"o\">.</span>domain<span class=\"p\">,</span>\n        <span class=\"p\">[</span>user<span class=\"o\">.</span>email<span class=\"p\">],</span>\n    <span class=\"p\">)</span>\n\n    <span class=\"c1\"># ...</span>\n</code></pre></div>\n<p>On Lawrence.com, this email has the subject line &quot;Thanks for subscribing to\nlawrence.com alerts.&quot; On LJWorld.com, the email has the subject &quot;Thanks for\nsubscribing to LJWorld.com alerts.&quot; Same goes for the email's message body.</p>\n<p>Note that an even more flexible (but more heavyweight) way of doing this would\nbe to use Django's template system. Assuming Lawrence.com and LJWorld.com have\ndifferent template directories (<a class=\"reference internal\" href=\"/id/1.11/ref/settings/#std-setting-TEMPLATES-DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">DIRS</code></a>), you could\nsimply farm out to the template system like so:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.core.mail</span> <span class=\"kn\">import</span> send_mail\n<span class=\"kn\">from</span> <span class=\"nn\">django.template</span> <span class=\"kn\">import</span> loader<span class=\"p\">,</span> Context\n\n<span class=\"k\">def</span> <span class=\"nf\">register_for_newsletter</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"c1\"># Check form values, etc., and subscribe the user.</span>\n    <span class=\"c1\"># ...</span>\n\n    subject <span class=\"o\">=</span> loader<span class=\"o\">.</span>get_template<span class=\"p\">(</span><span class=\"s1\">&#39;alerts/subject.txt&#39;</span><span class=\"p\">)</span><span class=\"o\">.</span>render<span class=\"p\">(</span>Context<span class=\"p\">({}))</span>\n    message <span class=\"o\">=</span> loader<span class=\"o\">.</span>get_template<span class=\"p\">(</span><span class=\"s1\">&#39;alerts/message.txt&#39;</span><span class=\"p\">)</span><span class=\"o\">.</span>render<span class=\"p\">(</span>Context<span class=\"p\">({}))</span>\n    send_mail<span class=\"p\">(</span>subject<span class=\"p\">,</span> message<span class=\"p\">,</span> <span class=\"s1\">&#39;editor@ljworld.com&#39;</span><span class=\"p\">,</span> <span class=\"p\">[</span>user<span class=\"o\">.</span>email<span class=\"p\">])</span>\n\n    <span class=\"c1\"># ...</span>\n</code></pre></div>\n<p>In this case, you'd have to create <code class=\"file docutils literal notranslate\">subject.txt</code> and <code class=\"file docutils literal notranslate\">message.txt</code>\ntemplate files for both the LJWorld.com and Lawrence.com template directories.\nThat gives you more flexibility, but it's also more complex.</p>\n<p>It's a good idea to exploit the <a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a>\nobjects as much as possible, to remove unneeded complexity and redundancy.</p>\n</section>\n<section id=\"getting-the-current-domain-for-full-urls\">\n<h3>Mendapatkan ranah saat ini untuk URL penuh<a class=\"heading-anchor\" href=\"#getting-the-current-domain-for-full-urls\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django's <code class=\"docutils literal notranslate\">get_absolute_url()</code> convention is nice for getting your objects'\nURL without the domain name, but in some cases you might want to display the\nfull URL -- with <code class=\"docutils literal notranslate\">http://</code> and the domain and everything -- for an object.\nTo do this, you can use the sites framework. A simple example:</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=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sites.models</span> <span class=\"kn\">import</span> Site\n<span class=\"gp\">&gt;&gt;&gt; </span>obj <span class=\"o\">=</span> MyModel<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"nb\">id</span><span class=\"o\">=</span><span class=\"mi\">3</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>obj<span class=\"o\">.</span>get_absolute_url<span class=\"p\">()</span>\n<span class=\"go\">&#39;/mymodel/objects/3/&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>Site<span class=\"o\">.</span>objects<span class=\"o\">.</span>get_current<span class=\"p\">()</span><span class=\"o\">.</span>domain\n<span class=\"go\">&#39;example.com&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"s1\">&#39;https://</span><span class=\"si\">%s%s</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"p\">(</span>Site<span class=\"o\">.</span>objects<span class=\"o\">.</span>get_current<span class=\"p\">()</span><span class=\"o\">.</span>domain<span class=\"p\">,</span> obj<span class=\"o\">.</span>get_absolute_url<span class=\"p\">())</span>\n<span class=\"go\">&#39;https://example.com/mymodel/objects/3/&#39;</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"enabling-the-sites-framework\">\n<span id=\"id1\"></span><h2>Mengadakan kerangka situs<a class=\"heading-anchor\" href=\"#enabling-the-sites-framework\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Untuk mengadakan kerangka situs ini, ikuti langkah-langkah ini:</p>\n<ol class=\"arabic\">\n<li><p>Menambahkan <code class=\"docutils literal notranslate\">'django.contrib.sites'</code> ke pengaturan <a class=\"reference internal\" href=\"/id/1.11/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> anda.</p></li>\n<li><p>Menentukan pengaturan <a class=\"reference internal\" href=\"/id/1.11/ref/settings/#std-setting-SITE_ID\"><code class=\"xref std std-setting docutils literal notranslate\">SITE_ID</code></a>:</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>SITE_ID <span class=\"o\">=</span> <span class=\"mi\">1</span>\n</code></pre></div>\n</li>\n<li><p>Menjalankan <a class=\"reference internal\" href=\"/id/1.11/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a>.</p></li>\n</ol>\n<p><code class=\"docutils literal notranslate\">django.contrib.sites</code> registers a\n<a class=\"reference internal\" href=\"/id/1.11/ref/signals/#django.db.models.signals.post_migrate\" title=\"django.db.models.signals.post_migrate\"><code class=\"xref py py-data docutils literal notranslate\">post_migrate</code></a> signal handler which creates a\ndefault site named <code class=\"docutils literal notranslate\">example.com</code> with the domain <code class=\"docutils literal notranslate\">example.com</code>. This site\nwill also be created after Django creates the test database. To set the\ncorrect name and domain for your project, you can use a <a class=\"reference internal\" href=\"/id/1.11/topics/migrations/#data-migrations\"><span class=\"std std-ref\">data migration</span></a>.</p>\n<p>In order to serve different sites in production, you'd create a separate\nsettings file with each <code class=\"docutils literal notranslate\">SITE_ID</code> (perhaps importing from a common settings\nfile to avoid duplicating shared settings) and then specify the appropriate\n<span class=\"target\" id=\"index-0\"></span><a class=\"reference internal\" href=\"/id/1.11/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\">DJANGO_SETTINGS_MODULE</code></a> for each site.</p>\n</section>\n<section id=\"caching-the-current-site-object\">\n<h2>Caching the current <code class=\"docutils literal notranslate\">Site</code> object<a class=\"heading-anchor\" href=\"#caching-the-current-site-object\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>As the current site is stored in the database, each call to\n<code class=\"docutils literal notranslate\">Site.objects.get_current()</code> could result in a database query. But Django is a\nlittle cleverer than that: on the first request, the current site is cached, and\nany subsequent call returns the cached data instead of hitting the database.</p>\n<p>If for any reason you want to force a database query, you can tell Django to\nclear the cache using <code class=\"docutils literal notranslate\">Site.objects.clear_cache()</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=\"c1\"># First call; current site fetched from database.</span>\ncurrent_site <span class=\"o\">=</span> Site<span class=\"o\">.</span>objects<span class=\"o\">.</span>get_current<span class=\"p\">()</span>\n<span class=\"c1\"># ...</span>\n\n<span class=\"c1\"># Second call; current site fetched from cache.</span>\ncurrent_site <span class=\"o\">=</span> Site<span class=\"o\">.</span>objects<span class=\"o\">.</span>get_current<span class=\"p\">()</span>\n<span class=\"c1\"># ...</span>\n\n<span class=\"c1\"># Force a database query for the third call.</span>\nSite<span class=\"o\">.</span>objects<span class=\"o\">.</span>clear_cache<span class=\"p\">()</span>\ncurrent_site <span class=\"o\">=</span> Site<span class=\"o\">.</span>objects<span class=\"o\">.</span>get_current<span class=\"p\">()</span>\n</code></pre></div>\n</section>\n<section id=\"the-currentsitemanager\">\n<h2><code class=\"docutils literal notranslate\">CurrentSiteManager</code><a class=\"heading-anchor\" href=\"#the-currentsitemanager\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sites.managers.CurrentSiteManager\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">managers.</span><span class=\"sig-name descname\">CurrentSiteManager</span><a class=\"heading-anchor\" href=\"#django.contrib.sites.managers.CurrentSiteManager\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If <a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> plays a key role in your\napplication, consider using the helpful\n<a class=\"reference internal\" href=\"#django.contrib.sites.managers.CurrentSiteManager\" title=\"django.contrib.sites.managers.CurrentSiteManager\"><code class=\"xref py py-class docutils literal notranslate\">CurrentSiteManager</code></a> in your\nmodel(s). It's a model <a class=\"reference internal\" href=\"/id/1.11/topics/db/managers/\"><span class=\"doc\">manager</span></a> that\nautomatically filters its queries to include only objects associated\nwith the current <a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a>.</p>\n<aside class=\"admonition-mandatory-setting-site-id admonition\" role=\"note\">\n<p class=\"admonition-title\"><a class=\"reference internal\" href=\"/id/1.11/ref/settings/#std-setting-SITE_ID\"><code class=\"xref std std-setting docutils literal notranslate\">SITE_ID</code></a> wajib</p>\n<p>The <code class=\"docutils literal notranslate\">CurrentSiteManager</code> is only usable when the <a class=\"reference internal\" href=\"/id/1.11/ref/settings/#std-setting-SITE_ID\"><code class=\"xref std std-setting docutils literal notranslate\">SITE_ID</code></a>\nsetting is defined in your settings.</p>\n</aside>\n<p>Use <a class=\"reference internal\" href=\"#django.contrib.sites.managers.CurrentSiteManager\" title=\"django.contrib.sites.managers.CurrentSiteManager\"><code class=\"xref py py-class docutils literal notranslate\">CurrentSiteManager</code></a> by adding it to\nyour model explicitly. For example:</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.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sites.models</span> <span class=\"kn\">import</span> Site\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sites.managers</span> <span class=\"kn\">import</span> CurrentSiteManager\n\n<span class=\"k\">class</span> <span class=\"nc\">Photo</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    photo <span class=\"o\">=</span> models<span class=\"o\">.</span>FileField<span class=\"p\">(</span>upload_to<span class=\"o\">=</span><span class=\"s1\">&#39;photos&#39;</span><span class=\"p\">)</span>\n    photographer_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">100</span><span class=\"p\">)</span>\n    pub_date <span class=\"o\">=</span> models<span class=\"o\">.</span>DateField<span class=\"p\">()</span>\n    site <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>Site<span class=\"p\">,</span> on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">)</span>\n    objects <span class=\"o\">=</span> models<span class=\"o\">.</span>Manager<span class=\"p\">()</span>\n    on_site <span class=\"o\">=</span> CurrentSiteManager<span class=\"p\">()</span>\n</code></pre></div>\n<p>With this model, <code class=\"docutils literal notranslate\">Photo.objects.all()</code> will return all <code class=\"docutils literal notranslate\">Photo</code> objects in\nthe database, but <code class=\"docutils literal notranslate\">Photo.on_site.all()</code> will return only the <code class=\"docutils literal notranslate\">Photo</code> objects\nassociated with the current site, according to the <a class=\"reference internal\" href=\"/id/1.11/ref/settings/#std-setting-SITE_ID\"><code class=\"xref std std-setting docutils literal notranslate\">SITE_ID</code></a> setting.</p>\n<p>Taruh di jalan lain, dua pernyataan ini adalah sama:</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>Photo<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>site<span class=\"o\">=</span>settings<span class=\"o\">.</span>SITE_ID<span class=\"p\">)</span>\nPhoto<span class=\"o\">.</span>on_site<span class=\"o\">.</span>all<span class=\"p\">()</span>\n</code></pre></div>\n<p>How did <a class=\"reference internal\" href=\"#django.contrib.sites.managers.CurrentSiteManager\" title=\"django.contrib.sites.managers.CurrentSiteManager\"><code class=\"xref py py-class docutils literal notranslate\">CurrentSiteManager</code></a>\nknow which field of <code class=\"docutils literal notranslate\">Photo</code> was the\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a>? By default,\n<a class=\"reference internal\" href=\"#django.contrib.sites.managers.CurrentSiteManager\" title=\"django.contrib.sites.managers.CurrentSiteManager\"><code class=\"xref py py-class docutils literal notranslate\">CurrentSiteManager</code></a> looks for a\neither a <a class=\"reference internal\" href=\"/id/1.11/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> called\n<code class=\"docutils literal notranslate\">site</code> or a\n<a class=\"reference internal\" href=\"/id/1.11/ref/models/fields/#django.db.models.ManyToManyField\" title=\"django.db.models.ManyToManyField\"><code class=\"xref py py-class docutils literal notranslate\">ManyToManyField</code></a> called\n<code class=\"docutils literal notranslate\">sites</code> to filter on. If you use a field named something other than\n<code class=\"docutils literal notranslate\">site</code> or <code class=\"docutils literal notranslate\">sites</code> to identify which\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> objects your object is\nrelated to, then you need to explicitly pass the custom field name as\na parameter to\n<a class=\"reference internal\" href=\"#django.contrib.sites.managers.CurrentSiteManager\" title=\"django.contrib.sites.managers.CurrentSiteManager\"><code class=\"xref py py-class docutils literal notranslate\">CurrentSiteManager</code></a> on your\nmodel. The following model, which has a field called <code class=\"docutils literal notranslate\">publish_on</code>,\ndemonstrates this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sites.models</span> <span class=\"kn\">import</span> Site\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sites.managers</span> <span class=\"kn\">import</span> CurrentSiteManager\n\n<span class=\"k\">class</span> <span class=\"nc\">Photo</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    photo <span class=\"o\">=</span> models<span class=\"o\">.</span>FileField<span class=\"p\">(</span>upload_to<span class=\"o\">=</span><span class=\"s1\">&#39;photos&#39;</span><span class=\"p\">)</span>\n    photographer_name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">100</span><span class=\"p\">)</span>\n    pub_date <span class=\"o\">=</span> models<span class=\"o\">.</span>DateField<span class=\"p\">()</span>\n    publish_on <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>Site<span class=\"p\">,</span> on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">)</span>\n    objects <span class=\"o\">=</span> models<span class=\"o\">.</span>Manager<span class=\"p\">()</span>\n    on_site <span class=\"o\">=</span> CurrentSiteManager<span class=\"p\">(</span><span class=\"s1\">&#39;publish_on&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>If you attempt to use <a class=\"reference internal\" href=\"#django.contrib.sites.managers.CurrentSiteManager\" title=\"django.contrib.sites.managers.CurrentSiteManager\"><code class=\"xref py py-class docutils literal notranslate\">CurrentSiteManager</code></a>\nand pass a field name that doesn't exist, Django will raise a <code class=\"docutils literal notranslate\">ValueError</code>.</p>\n<p>Finally, note that you'll probably want to keep a normal\n(non-site-specific) <code class=\"docutils literal notranslate\">Manager</code> on your model, even if you use\n<a class=\"reference internal\" href=\"#django.contrib.sites.managers.CurrentSiteManager\" title=\"django.contrib.sites.managers.CurrentSiteManager\"><code class=\"xref py py-class docutils literal notranslate\">CurrentSiteManager</code></a>. As\nexplained in the <a class=\"reference internal\" href=\"/id/1.11/topics/db/managers/\"><span class=\"doc\">manager documentation</span></a>, if\nyou define a manager manually, then Django won't create the automatic\n<code class=\"docutils literal notranslate\">objects = models.Manager()</code> manager for you. Also note that certain\nparts of Django -- namely, the Django admin site and generic views --\nuse whichever manager is defined <em>first</em> in the model, so if you want\nyour admin site to have access to all objects (not just site-specific\nones), put <code class=\"docutils literal notranslate\">objects = models.Manager()</code> in your model, before you\ndefine <a class=\"reference internal\" href=\"#django.contrib.sites.managers.CurrentSiteManager\" title=\"django.contrib.sites.managers.CurrentSiteManager\"><code class=\"xref py py-class docutils literal notranslate\">CurrentSiteManager</code></a>.</p>\n</section>\n<section id=\"site-middleware\">\n<span id=\"id2\"></span><h2>Situs middleware<a class=\"heading-anchor\" href=\"#site-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Jika anda sering menggunakan pola 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.contrib.sites.models</span> <span class=\"kn\">import</span> Site\n\n<span class=\"k\">def</span> <span class=\"nf\">my_view</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    site <span class=\"o\">=</span> Site<span class=\"o\">.</span>objects<span class=\"o\">.</span>get_current<span class=\"p\">()</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n<p>there is simple way to avoid repetitions. Add\n<a class=\"reference internal\" href=\"/id/1.11/ref/middleware/#django.contrib.sites.middleware.CurrentSiteMiddleware\" title=\"django.contrib.sites.middleware.CurrentSiteMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">django.contrib.sites.middleware.CurrentSiteMiddleware</code></a> to\n<a class=\"reference internal\" href=\"/id/1.11/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\">MIDDLEWARE</code></a>. The middleware sets the <code class=\"docutils literal notranslate\">site</code> attribute on every\nrequest object, so you can use <code class=\"docutils literal notranslate\">request.site</code> to get the current site.</p>\n</section>\n<section id=\"how-django-uses-the-sites-framework\">\n<h2>Bagaimana Django menggunakan kerangka situs<a class=\"heading-anchor\" href=\"#how-django-uses-the-sites-framework\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Although it's not required that you use the sites framework, it's strongly\nencouraged, because Django takes advantage of it in a few places. Even if your\nDjango installation is powering only a single site, you should take the two\nseconds to create the site object with your <code class=\"docutils literal notranslate\">domain</code> and <code class=\"docutils literal notranslate\">name</code>, and point\nto its ID in your <a class=\"reference internal\" href=\"/id/1.11/ref/settings/#std-setting-SITE_ID\"><code class=\"xref std std-setting docutils literal notranslate\">SITE_ID</code></a> setting.</p>\n<p>Ini adalah bagaimana Django menggunakan kerangka situs:</p>\n<ul class=\"simple\">\n<li><p>In the <a class=\"reference internal\" href=\"/id/1.11/ref/contrib/redirects/#module-django.contrib.redirects\" title=\"django.contrib.redirects: A framework for managing redirects.\"><code class=\"xref py py-mod docutils literal notranslate\">redirects framework</code></a>, each\nredirect object is associated with a particular site. When Django searches\nfor a redirect, it takes into account the current site.</p></li>\n<li><p>Dalam <a class=\"reference internal\" href=\"/id/1.11/ref/contrib/flatpages/#module-django.contrib.flatpages\" title=\"django.contrib.flatpages: A framework for managing simple ?flat? HTML content in a database.\"><code class=\"xref py py-mod docutils literal notranslate\">flatpages framework</code></a>, setiap flatpage dihubungkan dengan situs tertentu. Ketika sebuah flatpage dibuat, anda menentukan <a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> nya, dan <a class=\"reference internal\" href=\"/id/1.11/ref/contrib/flatpages/#django.contrib.flatpages.middleware.FlatpageFallbackMiddleware\" title=\"django.contrib.flatpages.middleware.FlatpageFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">FlatpageFallbackMiddleware</code></a> memeriksa situs saat ini dalam mengambil flatpage untuk dimunculkan.</p></li>\n<li><p>In the <a class=\"reference internal\" href=\"/id/1.11/ref/contrib/syndication/#module-django.contrib.syndication\" title=\"django.contrib.syndication: A framework for generating syndication feeds, in RSS and Atom, quite easily.\"><code class=\"xref py py-mod docutils literal notranslate\">syndication framework</code></a>, the\ntemplates for <code class=\"docutils literal notranslate\">title</code> and <code class=\"docutils literal notranslate\">description</code> automatically have access to a\nvariable <code class=\"docutils literal notranslate\">{{ site }}</code>, which is the\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> object representing the current\nsite. Also, the hook for providing item URLs will use the <code class=\"docutils literal notranslate\">domain</code> from\nthe current <a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> object if you don't\nspecify a fully-qualified domain.</p></li>\n<li><p>In the <a class=\"reference internal\" href=\"/id/1.11/topics/auth/#module-django.contrib.auth\" title=\"django.contrib.auth: Django's authentication framework.\"><code class=\"xref py py-mod docutils literal notranslate\">authentication framework</code></a>,\n<a class=\"reference internal\" href=\"/id/1.11/topics/auth/default/#django.contrib.auth.views.LoginView\" title=\"django.contrib.auth.views.LoginView\"><code class=\"xref py py-class docutils literal notranslate\">django.contrib.auth.views.LoginView</code></a> passes the current\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> name to the template as\n<code class=\"docutils literal notranslate\">{{ site_name }}</code>.</p></li>\n<li><p>The shortcut view (<code class=\"docutils literal notranslate\">django.contrib.contenttypes.views.shortcut</code>)\nuses the domain of the current\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> object when calculating\nan object's URL.</p></li>\n<li><p>In the admin framework, the &quot;view on site&quot; link uses the current\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> to work out the domain for the\nsite that it will redirect to.</p></li>\n</ul>\n</section>\n<section id=\"requestsite-objects\">\n<h2>Obyek <code class=\"docutils literal notranslate\">RequestSite</code><a class=\"heading-anchor\" href=\"#requestsite-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p id=\"id3\">Some <a class=\"reference internal\" href=\"/id/1.11/ref/contrib/\"><span class=\"doc\">django.contrib</span></a> applications take advantage of\nthe sites framework but are architected in a way that doesn't <em>require</em> the\nsites framework to be installed in your database. (Some people don't want to,\nor just aren't <em>able</em> to install the extra database table that the sites\nframework requires.) For those cases, the framework provides a\n<a class=\"reference internal\" href=\"#django.contrib.sites.requests.RequestSite\" title=\"django.contrib.sites.requests.RequestSite\"><code class=\"xref py py-class docutils literal notranslate\">django.contrib.sites.requests.RequestSite</code></a> class, which can be used as\na fallback when the database-backed sites framework is not available.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sites.requests.RequestSite\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-prename descclassname\">requests.</span><span class=\"sig-name descname\">RequestSite</span><a class=\"heading-anchor\" href=\"#django.contrib.sites.requests.RequestSite\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A class that shares the primary interface of\n<a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> (i.e., it has\n<code class=\"docutils literal notranslate\">domain</code> and <code class=\"docutils literal notranslate\">name</code> attributes) but gets its data from a Django\n<a class=\"reference internal\" href=\"/id/1.11/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\">HttpRequest</code></a> object rather than from a database.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sites.requests.RequestSite.__init__\">\n<span class=\"sig-name descname\">__init__</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.sites.requests.RequestSite.__init__\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Setel atribut <code class=\"docutils literal notranslate\">name</code> dan <code class=\"docutils literal notranslate\">domain</code> ke nilai dari <a class=\"reference internal\" href=\"/id/1.11/ref/request-response/#django.http.HttpRequest.get_host\" title=\"django.http.HttpRequest.get_host\"><code class=\"xref py py-meth docutils literal notranslate\">get_host()</code></a>.</p>\n</dd></dl>\n\n</dd></dl>\n\n<p>Sebuah obyek <a class=\"reference internal\" href=\"#django.contrib.sites.requests.RequestSite\" title=\"django.contrib.sites.requests.RequestSite\"><code class=\"xref py py-class docutils literal notranslate\">RequestSite</code></a> mempunyai antarmuka mirip pada obyek <a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> biasa, kecuali metode <a class=\"reference internal\" href=\"#django.contrib.sites.requests.RequestSite.__init__\" title=\"django.contrib.sites.requests.RequestSite.__init__\"><code class=\"xref py py-meth docutils literal notranslate\">__init__()</code></a> nya mengambil sebuah obyek <a class=\"reference internal\" href=\"/id/1.11/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\">HttpRequest</code></a>.  Itu dapat menyimpulkan <code class=\"docutils literal notranslate\">domain</code> dan <code class=\"docutils literal notranslate\">name</code> dengan mencari domain permintaan. Itu mempunyai metode <code class=\"docutils literal notranslate\">save()</code> dan <code class=\"docutils literal notranslate\">delete()</code> untuk mencocokkan antarmuka dari <a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a>, tetapi metode-metode memunculkan <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#NotImplementedError\" title=\"(di Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\">NotImplementedError</code></a>.</p>\n</section>\n<section id=\"get-current-site-shortcut\">\n<h2>Jalan pintas <code class=\"docutils literal notranslate\">get_current_site</code><a class=\"heading-anchor\" href=\"#get-current-site-shortcut\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Akhirnya, untuk menghindari kode fallback berulang, kerangka kerja menyediakan sebuah fungsi <a class=\"reference internal\" href=\"#django.contrib.sites.shortcuts.get_current_site\" title=\"django.contrib.sites.shortcuts.get_current_site\"><code class=\"xref py py-func docutils literal notranslate\">django.contrib.sites.shortcuts.get_current_site()</code></a></p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sites.shortcuts.get_current_site\">\n<span class=\"sig-prename descclassname\">shortcuts.</span><span class=\"sig-name descname\">get_current_site</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.sites.shortcuts.get_current_site\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Sebuah fungsi yang memeriksa jika <code class=\"docutils literal notranslate\">django.contrib.sites</code> dipasang dan mengembalikan salah satu obyek <a class=\"reference internal\" href=\"#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">Site</code></a> object saat ini atau obyek <a class=\"reference internal\" href=\"#django.contrib.sites.requests.RequestSite\" title=\"django.contrib.sites.requests.RequestSite\"><code class=\"xref py py-class docutils literal notranslate\">RequestSite</code></a> berdasarkan pada permintaan. Itu mencari situs saat ini berdasarkan pada <a class=\"reference internal\" href=\"/id/1.11/ref/request-response/#django.http.HttpRequest.get_host\" title=\"django.http.HttpRequest.get_host\"><code class=\"xref py py-meth docutils literal notranslate\">request.get_host()</code></a> jika pengaturan <a class=\"reference internal\" href=\"/id/1.11/ref/settings/#std-setting-SITE_ID\"><code class=\"xref std std-setting docutils literal notranslate\">SITE_ID</code></a> tidak ditentukan.</p>\n<p>Both a domain and a port may be returned by <a class=\"reference internal\" href=\"/id/1.11/ref/request-response/#django.http.HttpRequest.get_host\" title=\"django.http.HttpRequest.get_host\"><code class=\"xref py py-meth docutils literal notranslate\">request.get_host()</code></a> when the Host header has a port\nexplicitly specified, e.g. <code class=\"docutils literal notranslate\">example.com:80</code>. In such cases, if the\nlookup fails because the host does not match a record in the database,\nthe port is stripped and the lookup is retried with the domain part\nonly. This does not apply to\n<a class=\"reference internal\" href=\"#django.contrib.sites.requests.RequestSite\" title=\"django.contrib.sites.requests.RequestSite\"><code class=\"xref py py-class docutils literal notranslate\">RequestSite</code></a> which will always\nuse the unmodified host.</p>\n</dd></dl>\n\n</section>","rootId":"module-django.contrib.sites","toc":[{"title":"Example usage","anchor":"example-usage","children":[{"title":"Associating content with multiple sites","anchor":"associating-content-with-multiple-sites","children":[]},{"title":"Associating content with a single site","anchor":"associating-content-with-a-single-site","children":[]},{"title":"Mengaitkan kedalam situs saat ini dari tampilan","anchor":"hooking-into-the-current-site-from-views","children":[]},{"title":"Mendapatkan ranah saat ini untuk ditampilkan","anchor":"getting-the-current-domain-for-display","children":[]},{"title":"Mendapatkan ranah saat ini untuk URL penuh","anchor":"getting-the-current-domain-for-full-urls","children":[]}]},{"title":"Mengadakan kerangka situs","anchor":"enabling-the-sites-framework","children":[]},{"title":"Caching the current Site object","anchor":"caching-the-current-site-object","children":[]},{"title":"CurrentSiteManager","anchor":"the-currentsitemanager","children":[]},{"title":"Situs middleware","anchor":"site-middleware","children":[]},{"title":"Bagaimana Django menggunakan kerangka situs","anchor":"how-django-uses-the-sites-framework","children":[]},{"title":"Obyek RequestSite","anchor":"requestsite-objects","children":[]},{"title":"Jalan pintas get_current_site","anchor":"get-current-site-shortcut","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"Acuan API","url":"/id/1.11/ref/"},{"docname":"ref/contrib/index","title":"Paket contrib","url":"/id/1.11/ref/contrib/"}],"prev":{"docname":"ref/contrib/sitemaps","title":"Kerangka peta situs","url":"/id/1.11/ref/contrib/sitemaps/"},"next":{"docname":"ref/contrib/staticfiles","title":"Aplikasi staticfiles","url":"/id/1.11/ref/contrib/staticfiles/"},"formats":{"html":"/id/1.11/ref/contrib/sites/","markdown":"/id/1.11/ref/contrib/sites.md","json":"/id/1.11/ref/contrib/sites.json"},"source":"https://github.com/django/django/blob/stable/1.11.x/docs/ref/contrib/sites.txt","official":"https://docs.djangoproject.com/id/1.11/ref/contrib/sites/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","fr","ja","id","pt-br","ko","es","el","pl"]}