{"title":"The sitemap framework","version":"3.0","locale":"zh-hans","docname":"ref/contrib/sitemaps","url":"/zh-hans/3.0/ref/contrib/sitemaps/","canonical":"https://djangodocs.dev/zh-hans/3.0/ref/contrib/sitemaps/","summary":"Django comes with a high-level sitemap-generating framework to create sitemap XML files. 概况 Link to this heading # A sitemap is an XML file on your website that…","html":"<span id=\"the-sitemap-framework\"></span><h1>The sitemap framework<a class=\"heading-anchor\" href=\"#module-django.contrib.sitemaps\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django comes with a high-level sitemap-generating framework to create <a class=\"reference external\" href=\"https://www.sitemaps.org/\">sitemap</a>\nXML files.</p>\n<section id=\"overview\">\n<h2>概况<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A sitemap is an XML file on your website that tells search-engine indexers how\nfrequently your pages change and how &quot;important&quot; certain pages are in relation\nto other pages on your site. This information helps search engines index your\nsite.</p>\n<p>The Django sitemap framework automates the creation of this XML file by letting\nyou express this information in Python code.</p>\n<p>It works much like Django's <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/contrib/syndication/\"><span class=\"doc\">syndication framework</span></a>. To create a sitemap, write a\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap\" title=\"django.contrib.sitemaps.Sitemap\"><code class=\"xref py py-class docutils literal notranslate\">Sitemap</code></a> class and point to it in your\n<a class=\"reference internal\" href=\"/zh-hans/3.0/topics/http/urls/\"><span class=\"doc\">URLconf</span></a>.</p>\n</section>\n<section id=\"installation\">\n<h2>安装<a class=\"heading-anchor\" href=\"#installation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To install the sitemap app, follow these steps:</p>\n<ol class=\"arabic simple\">\n<li><p>Add <code class=\"docutils literal notranslate\">'django.contrib.sitemaps'</code> to your <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> setting.</p></li>\n<li><p>Make sure your <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\">TEMPLATES</code></a> setting contains a <code class=\"docutils literal notranslate\">DjangoTemplates</code>\nbackend whose <code class=\"docutils literal notranslate\">APP_DIRS</code> options is set to <code class=\"docutils literal notranslate\">True</code>. It's in there by\ndefault, so you'll only need to change this if you've changed that setting.</p></li>\n<li><p>Make sure you've installed the <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/contrib/sites/#module-django.contrib.sites\" title=\"django.contrib.sites: Lets you operate multiple websites from the same database and Django project\"><code class=\"xref py py-mod docutils literal notranslate\">sites framework</code></a>.</p></li>\n</ol>\n<p>(Note: The sitemap application doesn't install any database tables. The only\nreason it needs to go into <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> is so that the\n<a class=\"reference internal\" href=\"/zh-hans/3.0/ref/templates/api/#django.template.loaders.app_directories.Loader\" title=\"django.template.loaders.app_directories.Loader\"><code class=\"xref py py-func docutils literal notranslate\">Loader()</code></a> template\nloader can find the default templates.)</p>\n</section>\n<section id=\"initialization\">\n<h2>Initialization<a class=\"heading-anchor\" href=\"#initialization\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.views.sitemap\">\n<span class=\"sig-prename descclassname\">views.</span><span class=\"sig-name descname\">sitemap</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em>, <em class=\"sig-param\">sitemaps</em>, <em class=\"sig-param\">section<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">template_name<span class=\"o\">=</span><span class=\"default_value\">'sitemap.xml'</span></em>, <em class=\"sig-param\">content_type<span class=\"o\">=</span><span class=\"default_value\">'application/xml'</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.views.sitemap\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>To activate sitemap generation on your Django site, add this line to your\n<a class=\"reference internal\" href=\"/zh-hans/3.0/topics/http/urls/\"><span class=\"doc\">URLconf</span></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><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sitemaps.views</span> <span class=\"kn\">import</span> sitemap\n\npath<span class=\"p\">(</span><span class=\"s1\">&#39;sitemap.xml&#39;</span><span class=\"p\">,</span> sitemap<span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;sitemaps&#39;</span><span class=\"p\">:</span> sitemaps<span class=\"p\">},</span>\n     name<span class=\"o\">=</span><span class=\"s1\">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This tells Django to build a sitemap when a client accesses <code class=\"file docutils literal notranslate\">/sitemap.xml</code>.</p>\n<p>The name of the sitemap file is not important, but the location is. Search\nengines will only index links in your sitemap for the current URL level and\nbelow. For instance, if <code class=\"file docutils literal notranslate\">sitemap.xml</code> lives in your root directory, it may\nreference any URL in your site. However, if your sitemap lives at\n<code class=\"file docutils literal notranslate\">/content/sitemap.xml</code>, it may only reference URLs that begin with\n<code class=\"file docutils literal notranslate\">/content/</code>.</p>\n<p>The sitemap view takes an extra, required argument: <code class=\"docutils literal notranslate\">{'sitemaps': sitemaps}</code>.\n<code class=\"docutils literal notranslate\">sitemaps</code> should be a dictionary that maps a short section label (e.g.,\n<code class=\"docutils literal notranslate\">blog</code> or <code class=\"docutils literal notranslate\">news</code>) to its <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap\" title=\"django.contrib.sitemaps.Sitemap\"><code class=\"xref py py-class docutils literal notranslate\">Sitemap</code></a> class\n(e.g., <code class=\"docutils literal notranslate\">BlogSitemap</code> or <code class=\"docutils literal notranslate\">NewsSitemap</code>). It may also map to an <em>instance</em> of\na <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap\" title=\"django.contrib.sitemaps.Sitemap\"><code class=\"xref py py-class docutils literal notranslate\">Sitemap</code></a> class (e.g.,\n<code class=\"docutils literal notranslate\">BlogSitemap(some_var)</code>).</p>\n</section>\n<section id=\"sitemap-classes\">\n<h2><code class=\"docutils literal notranslate\">Sitemap</code> classes<a class=\"heading-anchor\" href=\"#sitemap-classes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap\" title=\"django.contrib.sitemaps.Sitemap\"><code class=\"xref py py-class docutils literal notranslate\">Sitemap</code></a> class is a Python class that\nrepresents a &quot;section&quot; of entries in your sitemap. For example, one\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap\" title=\"django.contrib.sitemaps.Sitemap\"><code class=\"xref py py-class docutils literal notranslate\">Sitemap</code></a> class could represent all the entries\nof your Weblog, while another could represent all of the events in your events\ncalendar.</p>\n<p>In the simplest case, all these sections get lumped together into one\n<code class=\"file docutils literal notranslate\">sitemap.xml</code>, but it's also possible to use the framework to generate a\nsitemap index that references individual sitemap files, one per section. (See\n<a class=\"reference internal\" href=\"#creating-a-sitemap-index\">Creating a sitemap index</a> below.)</p>\n<p><a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap\" title=\"django.contrib.sitemaps.Sitemap\"><code class=\"xref py py-class docutils literal notranslate\">Sitemap</code></a> classes must subclass\n<code class=\"docutils literal notranslate\">django.contrib.sitemaps.Sitemap</code>. They can live anywhere in your codebase.</p>\n</section>\n<section id=\"an-example\">\n<h2>一个例子<a class=\"heading-anchor\" href=\"#an-example\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Let's assume you have a blog system, with an <code class=\"docutils literal notranslate\">Entry</code> model, and you want your\nsitemap to include all the links to your individual blog entries. Here's how\nyour sitemap class might look:</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.sitemaps</span> <span class=\"kn\">import</span> Sitemap\n<span class=\"kn\">from</span> <span class=\"nn\">blog.models</span> <span class=\"kn\">import</span> Entry\n\n<span class=\"k\">class</span> <span class=\"nc\">BlogSitemap</span><span class=\"p\">(</span>Sitemap<span class=\"p\">):</span>\n    changefreq <span class=\"o\">=</span> <span class=\"s2\">&quot;never&quot;</span>\n    priority <span class=\"o\">=</span> <span class=\"mf\">0.5</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">items</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>is_draft<span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">lastmod</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> obj<span class=\"p\">):</span>\n        <span class=\"k\">return</span> obj<span class=\"o\">.</span>pub_date\n</code></pre></div>\n<p>Note:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.changefreq\" title=\"django.contrib.sitemaps.Sitemap.changefreq\"><code class=\"xref py py-attr docutils literal notranslate\">changefreq</code></a> and <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.priority\" title=\"django.contrib.sitemaps.Sitemap.priority\"><code class=\"xref py py-attr docutils literal notranslate\">priority</code></a> are class\nattributes corresponding to <code class=\"docutils literal notranslate\">&lt;changefreq&gt;</code> and <code class=\"docutils literal notranslate\">&lt;priority&gt;</code> elements,\nrespectively. They can be made callable as functions, as\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.lastmod\" title=\"django.contrib.sitemaps.Sitemap.lastmod\"><code class=\"xref py py-attr docutils literal notranslate\">lastmod</code></a> was in the example.</p></li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.items\" title=\"django.contrib.sitemaps.Sitemap.items\"><code class=\"xref py py-attr docutils literal notranslate\">items()</code></a> is a method that returns a <a class=\"reference external\" href=\"https://docs.python.org/3/glossary.html#term-sequence\" title=\"(in Python v3.14)\"><span class=\"xref std std-term\">sequence</span></a> or\n<code class=\"docutils literal notranslate\">QuerySet</code> of objects. The objects returned will get passed to any callable\nmethods corresponding to a sitemap property (<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.location\" title=\"django.contrib.sitemaps.Sitemap.location\"><code class=\"xref py py-attr docutils literal notranslate\">location</code></a>,\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.lastmod\" title=\"django.contrib.sitemaps.Sitemap.lastmod\"><code class=\"xref py py-attr docutils literal notranslate\">lastmod</code></a>, <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.changefreq\" title=\"django.contrib.sitemaps.Sitemap.changefreq\"><code class=\"xref py py-attr docutils literal notranslate\">changefreq</code></a>, and\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.priority\" title=\"django.contrib.sitemaps.Sitemap.priority\"><code class=\"xref py py-attr docutils literal notranslate\">priority</code></a>).</p></li>\n<li><p><a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.lastmod\" title=\"django.contrib.sitemaps.Sitemap.lastmod\"><code class=\"xref py py-attr docutils literal notranslate\">lastmod</code></a> should return a <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.datetime\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">datetime</code></a>.</p></li>\n<li><p>There is no <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.location\" title=\"django.contrib.sitemaps.Sitemap.location\"><code class=\"xref py py-attr docutils literal notranslate\">location</code></a> method in this example, but you\ncan provide it in order to specify the URL for your object. By default,\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.location\" title=\"django.contrib.sitemaps.Sitemap.location\"><code class=\"xref py py-attr docutils literal notranslate\">location()</code></a> calls <code class=\"docutils literal notranslate\">get_absolute_url()</code> on each object\nand returns the result.</p></li>\n</ul>\n</section>\n<section id=\"sitemap-class-reference\">\n<h2><code class=\"docutils literal notranslate\">Sitemap</code> class reference<a class=\"heading-anchor\" href=\"#sitemap-class-reference\"><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.sitemaps.Sitemap\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Sitemap</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.Sitemap\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A <code class=\"docutils literal notranslate\">Sitemap</code> class can define the following methods/attributes:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.Sitemap.items\">\n<span class=\"sig-name descname\">items</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.Sitemap.items\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><strong>Required.</strong> A method that returns a <a class=\"reference external\" href=\"https://docs.python.org/3/glossary.html#term-sequence\" title=\"(in Python v3.14)\"><span class=\"xref std std-term\">sequence</span></a> or <code class=\"docutils literal notranslate\">QuerySet</code>\nof objects. The framework doesn't care what <em>type</em> of objects they are;\nall that matters is that these objects get passed to the\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.location\" title=\"django.contrib.sitemaps.Sitemap.location\"><code class=\"xref py py-attr docutils literal notranslate\">location()</code></a>, <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.lastmod\" title=\"django.contrib.sitemaps.Sitemap.lastmod\"><code class=\"xref py py-attr docutils literal notranslate\">lastmod()</code></a>,\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.changefreq\" title=\"django.contrib.sitemaps.Sitemap.changefreq\"><code class=\"xref py py-attr docutils literal notranslate\">changefreq()</code></a> and <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.priority\" title=\"django.contrib.sitemaps.Sitemap.priority\"><code class=\"xref py py-attr docutils literal notranslate\">priority()</code></a> methods.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.Sitemap.location\">\n<span class=\"sig-name descname\">location</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.Sitemap.location\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><strong>Optional.</strong> Either a method or attribute.</p>\n<p>If it's a method, it should return the absolute path for a given object\nas returned by <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.items\" title=\"django.contrib.sitemaps.Sitemap.items\"><code class=\"xref py py-attr docutils literal notranslate\">items()</code></a>.</p>\n<p>If it's an attribute, its value should be a string representing an\nabsolute path to use for <em>every</em> object returned by\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.items\" title=\"django.contrib.sitemaps.Sitemap.items\"><code class=\"xref py py-attr docutils literal notranslate\">items()</code></a>.</p>\n<p>In both cases, &quot;absolute path&quot; means a URL that doesn't include the\nprotocol or domain. Examples:</p>\n<ul class=\"simple\">\n<li><p>Good: <code class=\"file docutils literal notranslate\">'/foo/bar/'</code></p></li>\n<li><p>Bad: <code class=\"file docutils literal notranslate\">'example.com/foo/bar/'</code></p></li>\n<li><p>Bad: <code class=\"file docutils literal notranslate\">'https://example.com/foo/bar/'</code></p></li>\n</ul>\n<p>If <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.location\" title=\"django.contrib.sitemaps.Sitemap.location\"><code class=\"xref py py-attr docutils literal notranslate\">location</code></a> isn't provided, the framework will call\nthe <code class=\"docutils literal notranslate\">get_absolute_url()</code> method on each object as returned by\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.items\" title=\"django.contrib.sitemaps.Sitemap.items\"><code class=\"xref py py-attr docutils literal notranslate\">items()</code></a>.</p>\n<p>To specify a protocol other than <code class=\"docutils literal notranslate\">'http'</code>, use\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.protocol\" title=\"django.contrib.sitemaps.Sitemap.protocol\"><code class=\"xref py py-attr docutils literal notranslate\">protocol</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.Sitemap.lastmod\">\n<span class=\"sig-name descname\">lastmod</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.Sitemap.lastmod\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><strong>Optional.</strong> Either a method or attribute.</p>\n<p>If it's a method, it should take one argument -- an object as returned\nby <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.items\" title=\"django.contrib.sitemaps.Sitemap.items\"><code class=\"xref py py-attr docutils literal notranslate\">items()</code></a> -- and return that object's last-modified\ndate/time as a <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.datetime\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">datetime</code></a>.</p>\n<p>If it's an attribute, its value should be a <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.datetime\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">datetime</code></a>\nrepresenting the last-modified date/time for <em>every</em> object returned by\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.items\" title=\"django.contrib.sitemaps.Sitemap.items\"><code class=\"xref py py-attr docutils literal notranslate\">items()</code></a>.</p>\n<p>If all items in a sitemap have a <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.lastmod\" title=\"django.contrib.sitemaps.Sitemap.lastmod\"><code class=\"xref py py-attr docutils literal notranslate\">lastmod</code></a>, the sitemap\ngenerated by <a class=\"reference internal\" href=\"#django.contrib.sitemaps.views.sitemap\" title=\"django.contrib.sitemaps.views.sitemap\"><code class=\"xref py py-func docutils literal notranslate\">views.sitemap()</code></a> will have a <code class=\"docutils literal notranslate\">Last-Modified</code>\nheader equal to the latest <code class=\"docutils literal notranslate\">lastmod</code>. You can activate the\n<a class=\"reference internal\" href=\"/zh-hans/3.0/ref/middleware/#django.middleware.http.ConditionalGetMiddleware\" title=\"django.middleware.http.ConditionalGetMiddleware\"><code class=\"xref py py-class docutils literal notranslate\">ConditionalGetMiddleware</code></a> to make\nDjango respond appropriately to requests with an <code class=\"docutils literal notranslate\">If-Modified-Since</code>\nheader which will prevent sending the sitemap if it hasn't changed.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.Sitemap.changefreq\">\n<span class=\"sig-name descname\">changefreq</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.Sitemap.changefreq\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><strong>Optional.</strong> Either a method or attribute.</p>\n<p>If it's a method, it should take one argument -- an object as returned\nby <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.items\" title=\"django.contrib.sitemaps.Sitemap.items\"><code class=\"xref py py-attr docutils literal notranslate\">items()</code></a> -- and return that object's change\nfrequency as a string.</p>\n<p>If it's an attribute, its value should be a string representing the\nchange frequency of <em>every</em> object returned by <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.items\" title=\"django.contrib.sitemaps.Sitemap.items\"><code class=\"xref py py-attr docutils literal notranslate\">items()</code></a>.</p>\n<p>Possible values for <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.changefreq\" title=\"django.contrib.sitemaps.Sitemap.changefreq\"><code class=\"xref py py-attr docutils literal notranslate\">changefreq</code></a>, whether you use a\nmethod or attribute, are:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">'always'</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">'hourly'</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">'daily'</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">'weekly'</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">'monthly'</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">'yearly'</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">'never'</code></p></li>\n</ul>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.Sitemap.priority\">\n<span class=\"sig-name descname\">priority</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.Sitemap.priority\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><strong>Optional.</strong> Either a method or attribute.</p>\n<p>If it's a method, it should take one argument -- an object as returned\nby <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.items\" title=\"django.contrib.sitemaps.Sitemap.items\"><code class=\"xref py py-attr docutils literal notranslate\">items()</code></a> -- and return that object's priority as\neither a string or float.</p>\n<p>If it's an attribute, its value should be either a string or float\nrepresenting the priority of <em>every</em> object returned by\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.items\" title=\"django.contrib.sitemaps.Sitemap.items\"><code class=\"xref py py-attr docutils literal notranslate\">items()</code></a>.</p>\n<p>Example values for <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.priority\" title=\"django.contrib.sitemaps.Sitemap.priority\"><code class=\"xref py py-attr docutils literal notranslate\">priority</code></a>: <code class=\"docutils literal notranslate\">0.4</code>, <code class=\"docutils literal notranslate\">1.0</code>. The\ndefault priority of a page is <code class=\"docutils literal notranslate\">0.5</code>. See the <a class=\"reference external\" href=\"https://www.sitemaps.org/protocol.html#prioritydef\">sitemaps.org\ndocumentation</a> for more.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.Sitemap.protocol\">\n<span class=\"sig-name descname\">protocol</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.Sitemap.protocol\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><strong>Optional.</strong></p>\n<p>This attribute defines the protocol (<code class=\"docutils literal notranslate\">'http'</code> or <code class=\"docutils literal notranslate\">'https'</code>) of the\nURLs in the sitemap. If it isn't set, the protocol with which the\nsitemap was requested is used. If the sitemap is built outside the\ncontext of a request, the default is <code class=\"docutils literal notranslate\">'http'</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.Sitemap.limit\">\n<span class=\"sig-name descname\">limit</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.Sitemap.limit\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><strong>Optional.</strong></p>\n<p>This attribute defines the maximum number of URLs included on each page\nof the sitemap. Its value should not exceed the default value of\n<code class=\"docutils literal notranslate\">50000</code>, which is the upper limit allowed in the <a class=\"reference external\" href=\"https://www.sitemaps.org/protocol.html#index\">Sitemaps protocol</a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.Sitemap.i18n\">\n<span class=\"sig-name descname\">i18n</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.Sitemap.i18n\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><strong>Optional.</strong></p>\n<p>A boolean attribute that defines if the URLs of this sitemap should\nbe generated using all of your <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/settings/#std-setting-LANGUAGES\"><code class=\"xref std std-setting docutils literal notranslate\">LANGUAGES</code></a>. The default is\n<code class=\"docutils literal notranslate\">False</code>.</p>\n</dd></dl>\n\n</dd></dl>\n\n</section>\n<section id=\"shortcuts\">\n<h2>Shortcuts<a class=\"heading-anchor\" href=\"#shortcuts\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The sitemap framework provides a convenience class for a common case:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.GenericSitemap\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">GenericSitemap</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">info_dict</em>, <em class=\"sig-param\">priority<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">changefreq<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">protocol<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.GenericSitemap\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <a class=\"reference internal\" href=\"#django.contrib.sitemaps.GenericSitemap\" title=\"django.contrib.sitemaps.GenericSitemap\"><code class=\"xref py py-class docutils literal notranslate\">django.contrib.sitemaps.GenericSitemap</code></a> class allows you to\ncreate a sitemap by passing it a dictionary which has to contain at least\na <code class=\"docutils literal notranslate\">queryset</code> entry. This queryset will be used to generate the items\nof the sitemap. It may also have a <code class=\"docutils literal notranslate\">date_field</code> entry that\nspecifies a date field for objects retrieved from the <code class=\"docutils literal notranslate\">queryset</code>.\nThis will be used for the <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.lastmod\" title=\"django.contrib.sitemaps.Sitemap.lastmod\"><code class=\"xref py py-attr docutils literal notranslate\">lastmod</code></a> attribute in the\ngenerated sitemap.</p>\n<p>The <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.priority\" title=\"django.contrib.sitemaps.Sitemap.priority\"><code class=\"xref py py-attr docutils literal notranslate\">priority</code></a>, <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.changefreq\" title=\"django.contrib.sitemaps.Sitemap.changefreq\"><code class=\"xref py py-attr docutils literal notranslate\">changefreq</code></a>,\nand <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.protocol\" title=\"django.contrib.sitemaps.Sitemap.protocol\"><code class=\"xref py py-attr docutils literal notranslate\">protocol</code></a> keyword arguments allow specifying these\nattributes for all URLs.</p>\n</dd></dl>\n\n<section id=\"example\">\n<h3>例如<a class=\"heading-anchor\" href=\"#example\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Here's an example of a <a class=\"reference internal\" href=\"/zh-hans/3.0/topics/http/urls/\"><span class=\"doc\">URLconf</span></a> using\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.GenericSitemap\" title=\"django.contrib.sitemaps.GenericSitemap\"><code class=\"xref py py-class docutils literal notranslate\">GenericSitemap</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><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sitemaps</span> <span class=\"kn\">import</span> GenericSitemap\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sitemaps.views</span> <span class=\"kn\">import</span> sitemap\n<span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> path\n<span class=\"kn\">from</span> <span class=\"nn\">blog.models</span> <span class=\"kn\">import</span> Entry\n\ninfo_dict <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s1\">&#39;queryset&#39;</span><span class=\"p\">:</span> Entry<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">(),</span>\n    <span class=\"s1\">&#39;date_field&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;pub_date&#39;</span><span class=\"p\">,</span>\n<span class=\"p\">}</span>\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"c1\"># some generic view using info_dict</span>\n    <span class=\"c1\"># ...</span>\n\n    <span class=\"c1\"># the sitemap</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;sitemap.xml&#39;</span><span class=\"p\">,</span> sitemap<span class=\"p\">,</span>\n         <span class=\"p\">{</span><span class=\"s1\">&#39;sitemaps&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span><span class=\"s1\">&#39;blog&#39;</span><span class=\"p\">:</span> GenericSitemap<span class=\"p\">(</span>info_dict<span class=\"p\">,</span> priority<span class=\"o\">=</span><span class=\"mf\">0.6</span><span class=\"p\">)}},</span>\n         name<span class=\"o\">=</span><span class=\"s1\">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"sitemap-for-static-views\">\n<h2>Sitemap for static views<a class=\"heading-anchor\" href=\"#sitemap-for-static-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Often you want the search engine crawlers to index views which are neither\nobject detail pages nor flatpages. The solution is to explicitly list URL\nnames for these views in <code class=\"docutils literal notranslate\">items</code> and call <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/urlresolvers/#django.urls.reverse\" title=\"django.urls.reverse\"><code class=\"xref py py-func docutils literal notranslate\">reverse()</code></a> in\nthe <code class=\"docutils literal notranslate\">location</code> method of the sitemap. 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=\"c1\"># sitemaps.py</span>\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib</span> <span class=\"kn\">import</span> sitemaps\n<span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> reverse\n\n<span class=\"k\">class</span> <span class=\"nc\">StaticViewSitemap</span><span class=\"p\">(</span>sitemaps<span class=\"o\">.</span>Sitemap<span class=\"p\">):</span>\n    priority <span class=\"o\">=</span> <span class=\"mf\">0.5</span>\n    changefreq <span class=\"o\">=</span> <span class=\"s1\">&#39;daily&#39;</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">items</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"p\">[</span><span class=\"s1\">&#39;main&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;about&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;license&#39;</span><span class=\"p\">]</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">location</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> item<span class=\"p\">):</span>\n        <span class=\"k\">return</span> reverse<span class=\"p\">(</span>item<span class=\"p\">)</span>\n\n<span class=\"c1\"># urls.py</span>\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sitemaps.views</span> <span class=\"kn\">import</span> sitemap\n<span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> path\n\n<span class=\"kn\">from</span> <span class=\"nn\">.sitemaps</span> <span class=\"kn\">import</span> StaticViewSitemap\n<span class=\"kn\">from</span> <span class=\"nn\">.</span> <span class=\"kn\">import</span> views\n\nsitemaps <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s1\">&#39;static&#39;</span><span class=\"p\">:</span> StaticViewSitemap<span class=\"p\">,</span>\n<span class=\"p\">}</span>\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">,</span> views<span class=\"o\">.</span>main<span class=\"p\">,</span> name<span class=\"o\">=</span><span class=\"s1\">&#39;main&#39;</span><span class=\"p\">),</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;about/&#39;</span><span class=\"p\">,</span> views<span class=\"o\">.</span>about<span class=\"p\">,</span> name<span class=\"o\">=</span><span class=\"s1\">&#39;about&#39;</span><span class=\"p\">),</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;license/&#39;</span><span class=\"p\">,</span> views<span class=\"o\">.</span>license<span class=\"p\">,</span> name<span class=\"o\">=</span><span class=\"s1\">&#39;license&#39;</span><span class=\"p\">),</span>\n    <span class=\"c1\"># ...</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;sitemap.xml&#39;</span><span class=\"p\">,</span> sitemap<span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;sitemaps&#39;</span><span class=\"p\">:</span> sitemaps<span class=\"p\">},</span>\n         name<span class=\"o\">=</span><span class=\"s1\">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class=\"p\">)</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n</section>\n<section id=\"creating-a-sitemap-index\">\n<h2>Creating a sitemap index<a class=\"heading-anchor\" href=\"#creating-a-sitemap-index\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.views.index\">\n<span class=\"sig-prename descclassname\">views.</span><span class=\"sig-name descname\">index</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">request</em>, <em class=\"sig-param\">sitemaps</em>, <em class=\"sig-param\">template_name<span class=\"o\">=</span><span class=\"default_value\">'sitemap_index.xml'</span></em>, <em class=\"sig-param\">content_type<span class=\"o\">=</span><span class=\"default_value\">'application/xml'</span></em>, <em class=\"sig-param\">sitemap_url_name<span class=\"o\">=</span><span class=\"default_value\">'django.contrib.sitemaps.views.sitemap'</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.views.index\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The sitemap framework also has the ability to create a sitemap index that\nreferences individual sitemap files, one per each section defined in your\n<code class=\"docutils literal notranslate\">sitemaps</code> dictionary. The only differences in usage are:</p>\n<ul class=\"simple\">\n<li><p>You use two views in your URLconf: <a class=\"reference internal\" href=\"#django.contrib.sitemaps.views.index\" title=\"django.contrib.sitemaps.views.index\"><code class=\"xref py py-func docutils literal notranslate\">django.contrib.sitemaps.views.index()</code></a>\nand <a class=\"reference internal\" href=\"#django.contrib.sitemaps.views.sitemap\" title=\"django.contrib.sitemaps.views.sitemap\"><code class=\"xref py py-func docutils literal notranslate\">django.contrib.sitemaps.views.sitemap()</code></a>.</p></li>\n<li><p>The <a class=\"reference internal\" href=\"#django.contrib.sitemaps.views.sitemap\" title=\"django.contrib.sitemaps.views.sitemap\"><code class=\"xref py py-func docutils literal notranslate\">django.contrib.sitemaps.views.sitemap()</code></a> view should take a\n<code class=\"docutils literal notranslate\">section</code> keyword argument.</p></li>\n</ul>\n<p>Here's what the relevant URLconf lines would look like for the example above:</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.sitemaps</span> <span class=\"kn\">import</span> views\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;sitemap.xml&#39;</span><span class=\"p\">,</span> views<span class=\"o\">.</span>index<span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;sitemaps&#39;</span><span class=\"p\">:</span> sitemaps<span class=\"p\">}),</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;sitemap-&lt;section&gt;.xml&#39;</span><span class=\"p\">,</span> views<span class=\"o\">.</span>sitemap<span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;sitemaps&#39;</span><span class=\"p\">:</span> sitemaps<span class=\"p\">},</span>\n         name<span class=\"o\">=</span><span class=\"s1\">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>This will automatically generate a <code class=\"file docutils literal notranslate\">sitemap.xml</code> file that references\nboth <code class=\"file docutils literal notranslate\">sitemap-flatpages.xml</code> and <code class=\"file docutils literal notranslate\">sitemap-blog.xml</code>. The\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap\" title=\"django.contrib.sitemaps.Sitemap\"><code class=\"xref py py-class docutils literal notranslate\">Sitemap</code></a> classes and the <code class=\"docutils literal notranslate\">sitemaps</code>\ndict don't change at all.</p>\n<p>You should create an index file if one of your sitemaps has more than 50,000\nURLs. In this case, Django will automatically paginate the sitemap, and the\nindex will reflect that.</p>\n<p>If you're not using the vanilla sitemap view -- for example, if it's wrapped\nwith a caching decorator -- you must name your sitemap view and pass\n<code class=\"docutils literal notranslate\">sitemap_url_name</code> to the index view:</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.sitemaps</span> <span class=\"kn\">import</span> views <span class=\"k\">as</span> sitemaps_views\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.decorators.cache</span> <span class=\"kn\">import</span> cache_page\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;sitemap.xml&#39;</span><span class=\"p\">,</span>\n         cache_page<span class=\"p\">(</span><span class=\"mi\">86400</span><span class=\"p\">)(</span>sitemaps_views<span class=\"o\">.</span>index<span class=\"p\">),</span>\n         <span class=\"p\">{</span><span class=\"s1\">&#39;sitemaps&#39;</span><span class=\"p\">:</span> sitemaps<span class=\"p\">,</span> <span class=\"s1\">&#39;sitemap_url_name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;sitemaps&#39;</span><span class=\"p\">}),</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;sitemap-&lt;section&gt;.xml&#39;</span><span class=\"p\">,</span>\n         cache_page<span class=\"p\">(</span><span class=\"mi\">86400</span><span class=\"p\">)(</span>sitemaps_views<span class=\"o\">.</span>sitemap<span class=\"p\">),</span>\n         <span class=\"p\">{</span><span class=\"s1\">&#39;sitemaps&#39;</span><span class=\"p\">:</span> sitemaps<span class=\"p\">},</span> name<span class=\"o\">=</span><span class=\"s1\">&#39;sitemaps&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n</section>\n<section id=\"template-customization\">\n<h2>Template customization<a class=\"heading-anchor\" href=\"#template-customization\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If you wish to use a different template for each sitemap or sitemap index\navailable on your site, you may specify it by passing a <code class=\"docutils literal notranslate\">template_name</code>\nparameter to the <code class=\"docutils literal notranslate\">sitemap</code> and <code class=\"docutils literal notranslate\">index</code> views via the URLconf:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.sitemaps</span> <span class=\"kn\">import</span> views\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;custom-sitemap.xml&#39;</span><span class=\"p\">,</span> views<span class=\"o\">.</span>index<span class=\"p\">,</span> <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;sitemaps&#39;</span><span class=\"p\">:</span> sitemaps<span class=\"p\">,</span>\n        <span class=\"s1\">&#39;template_name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;custom_sitemap.html&#39;</span>\n    <span class=\"p\">}),</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;custom-sitemap-&lt;section&gt;.xml&#39;</span><span class=\"p\">,</span> views<span class=\"o\">.</span>sitemap<span class=\"p\">,</span> <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;sitemaps&#39;</span><span class=\"p\">:</span> sitemaps<span class=\"p\">,</span>\n        <span class=\"s1\">&#39;template_name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;custom_sitemap.html&#39;</span>\n    <span class=\"p\">},</span> name<span class=\"o\">=</span><span class=\"s1\">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>These views return <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/template-response/#django.template.response.TemplateResponse\" title=\"django.template.response.TemplateResponse\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponse</code></a>\ninstances which allow you to easily customize the response data before\nrendering. For more details, see the <a class=\"reference internal\" href=\"/zh-hans/3.0/ref/template-response/\"><span class=\"doc\">TemplateResponse documentation</span></a>.</p>\n<section id=\"context-variables\">\n<h3>Context variables<a class=\"heading-anchor\" href=\"#context-variables\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When customizing the templates for the\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.views.index\" title=\"django.contrib.sitemaps.views.index\"><code class=\"xref py py-func docutils literal notranslate\">index()</code></a> and\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.views.sitemap\" title=\"django.contrib.sitemaps.views.sitemap\"><code class=\"xref py py-func docutils literal notranslate\">sitemap()</code></a> views, you can rely on the\nfollowing context variables.</p>\n</section>\n<section id=\"index\">\n<h3>索引<a class=\"heading-anchor\" href=\"#index\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The variable <code class=\"docutils literal notranslate\">sitemaps</code> is a list of absolute URLs to each of the sitemaps.</p>\n</section>\n<section id=\"id1\">\n<h3>Sitemap<a class=\"heading-anchor\" href=\"#id1\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The variable <code class=\"docutils literal notranslate\">urlset</code> is a list of URLs that should appear in the\nsitemap. Each URL exposes attributes as defined in the\n<a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap\" title=\"django.contrib.sitemaps.Sitemap\"><code class=\"xref py py-class docutils literal notranslate\">Sitemap</code></a> class:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">changefreq</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">item</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">lastmod</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">location</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">priority</code></p></li>\n</ul>\n<p>The <code class=\"docutils literal notranslate\">item</code> attribute has been added for each URL to allow more flexible\ncustomization of the templates, such as <a class=\"reference external\" href=\"https://support.google.com/news/publisher/answer/74288?hl=en\">Google news sitemaps</a>. Assuming\nSitemap's <a class=\"reference internal\" href=\"#django.contrib.sitemaps.Sitemap.items\" title=\"django.contrib.sitemaps.Sitemap.items\"><code class=\"xref py py-attr docutils literal notranslate\">items()</code></a> would return a list of items with\n<code class=\"docutils literal notranslate\">publication_data</code> and a <code class=\"docutils literal notranslate\">tags</code> field something like this would\ngenerate a Google News compatible sitemap:</p>\n<div class=\"code-block\" data-language=\"xml+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Xml Django</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=\"Xml Django code\"><code><span class=\"cp\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>\n<span class=\"nt\">&lt;urlset</span>\n  <span class=\"na\">xmlns=</span><span class=\"s\">&quot;https://www.sitemaps.org/schemas/sitemap/0.9&quot;</span>\n  <span class=\"na\">xmlns:news=</span><span class=\"s\">&quot;http://www.google.com/schemas/sitemap-news/0.9&quot;</span><span class=\"nt\">&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">spaceless</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">url</span> <span class=\"k\">in</span> <span class=\"nv\">urlset</span> <span class=\"cp\">%}</span>\n  <span class=\"nt\">&lt;url&gt;</span>\n    <span class=\"nt\">&lt;loc&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">url.location</span> <span class=\"cp\">}}</span><span class=\"nt\">&lt;/loc&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">url.lastmod</span> <span class=\"cp\">%}</span><span class=\"nt\">&lt;lastmod&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">url.lastmod</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;Y-m-d&quot;</span> <span class=\"cp\">}}</span><span class=\"nt\">&lt;/lastmod&gt;</span><span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">url.changefreq</span> <span class=\"cp\">%}</span><span class=\"nt\">&lt;changefreq&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">url.changefreq</span> <span class=\"cp\">}}</span><span class=\"nt\">&lt;/changefreq&gt;</span><span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">url.priority</span> <span class=\"cp\">%}</span><span class=\"nt\">&lt;priority&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">url.priority</span> <span class=\"cp\">}}</span><span class=\"nt\">&lt;/priority&gt;</span><span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n    <span class=\"nt\">&lt;news:news&gt;</span>\n      <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">url.item.publication_date</span> <span class=\"cp\">%}</span><span class=\"nt\">&lt;news:publication_date&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">url.item.publication_date</span><span class=\"o\">|</span><span class=\"nf\">date</span><span class=\"s2\">:&quot;Y-m-d&quot;</span> <span class=\"cp\">}}</span><span class=\"nt\">&lt;/news:publication_date&gt;</span><span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n      <span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">url.item.tags</span> <span class=\"cp\">%}</span><span class=\"nt\">&lt;news:keywords&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">url.item.tags</span> <span class=\"cp\">}}</span><span class=\"nt\">&lt;/news:keywords&gt;</span><span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span>\n    <span class=\"nt\">&lt;/news:news&gt;</span>\n   <span class=\"nt\">&lt;/url&gt;</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endspaceless</span> <span class=\"cp\">%}</span>\n<span class=\"nt\">&lt;/urlset&gt;</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"pinging-google\">\n<h2>Pinging Google<a class=\"heading-anchor\" href=\"#pinging-google\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>You may want to &quot;ping&quot; Google when your sitemap changes, to let it know to\nreindex your site. The sitemaps framework provides a function to do just\nthat: <a class=\"reference internal\" href=\"#django.contrib.sitemaps.ping_google\" title=\"django.contrib.sitemaps.ping_google\"><code class=\"xref py py-func docutils literal notranslate\">django.contrib.sitemaps.ping_google()</code></a>.</p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.sitemaps.ping_google\">\n<span class=\"sig-name descname\">ping_google</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">sitemap_url<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">ping_url<span class=\"o\">=</span><span class=\"default_value\">PING_URL</span></em>, <em class=\"sig-param\">sitemap_uses_https<span class=\"o\">=</span><span class=\"default_value\">True</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.sitemaps.ping_google\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><code class=\"docutils literal notranslate\">ping_google</code> takes these optional arguments:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">sitemap_url</code> - The absolute path to your site's sitemap (e.g.,\n<code class=\"file docutils literal notranslate\">'/sitemap.xml'</code>). If this argument isn't provided, <code class=\"docutils literal notranslate\">ping_google</code>\nwill attempt to figure out your sitemap by performing a reverse lookup in\nyour URLconf.</p></li>\n<li><p><code class=\"docutils literal notranslate\">ping_url</code> - Defaults to Google's Ping Tool:\n<a class=\"reference external\" href=\"https://www.google.com/webmasters/tools/ping\">https://www.google.com/webmasters/tools/ping</a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\">sitemap_uses_https</code> - Set to <code class=\"docutils literal notranslate\">False</code> if your site uses <code class=\"docutils literal notranslate\">http</code>\nrather than <code class=\"docutils literal notranslate\">https</code>.</p></li>\n</ul>\n<p><a class=\"reference internal\" href=\"#django.contrib.sitemaps.ping_google\" title=\"django.contrib.sitemaps.ping_google\"><code class=\"xref py py-func docutils literal notranslate\">ping_google()</code></a> raises the exception\n<code class=\"docutils literal notranslate\">django.contrib.sitemaps.SitemapNotFound</code> if it cannot determine your\nsitemap URL.</p>\n<aside class=\"version-note version-added\" data-version=\"2.2\" role=\"note\">\n<p class=\"version-note-title\">New in Django 2.2</p><p>The <code class=\"docutils literal notranslate\">sitemap_uses_https</code> argument was added. Older versions of\nDjango always use <code class=\"docutils literal notranslate\">http</code> for a sitemap's URL.</p>\n</aside>\n</dd></dl>\n\n<aside class=\"admonition-register-with-google-first admonition\" role=\"note\">\n<p class=\"admonition-title\">Register with Google first!</p>\n<p>The <a class=\"reference internal\" href=\"#django.contrib.sitemaps.ping_google\" title=\"django.contrib.sitemaps.ping_google\"><code class=\"xref py py-func docutils literal notranslate\">ping_google()</code></a> command only works if you have registered your\nsite with <a class=\"reference external\" href=\"https://www.google.com/webmasters/tools/\">Google Webmaster Tools</a>.</p>\n</aside>\n<p>One useful way to call <a class=\"reference internal\" href=\"#django.contrib.sitemaps.ping_google\" title=\"django.contrib.sitemaps.ping_google\"><code class=\"xref py py-func docutils literal notranslate\">ping_google()</code></a> is from a model's <code class=\"docutils literal notranslate\">save()</code>\nmethod:</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.sitemaps</span> <span class=\"kn\">import</span> ping_google\n\n<span class=\"k\">class</span> <span class=\"nc\">Entry</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"k\">def</span> <span class=\"nf\">save</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> force_insert<span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">,</span> force_update<span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">):</span>\n        <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>save<span class=\"p\">(</span>force_insert<span class=\"p\">,</span> force_update<span class=\"p\">)</span>\n        <span class=\"k\">try</span><span class=\"p\">:</span>\n            ping_google<span class=\"p\">()</span>\n        <span class=\"k\">except</span> <span class=\"ne\">Exception</span><span class=\"p\">:</span>\n            <span class=\"c1\"># Bare &#39;except&#39; because we could get a variety</span>\n            <span class=\"c1\"># of HTTP-related exceptions.</span>\n            <span class=\"k\">pass</span>\n</code></pre></div>\n<p>A more efficient solution, however, would be to call <a class=\"reference internal\" href=\"#django.contrib.sitemaps.ping_google\" title=\"django.contrib.sitemaps.ping_google\"><code class=\"xref py py-func docutils literal notranslate\">ping_google()</code></a> from a\ncron script, or some other scheduled task. The function makes an HTTP request\nto Google's servers, so you may not want to introduce that network overhead\neach time you call <code class=\"docutils literal notranslate\">save()</code>.</p>\n<section id=\"pinging-google-via-manage-py\">\n<h3>Pinging Google via <code class=\"docutils literal notranslate\">manage.py</code><a class=\"heading-anchor\" href=\"#pinging-google-via-manage-py\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"std django-admin\">\n<dt class=\"sig sig-object std\" id=\"django-admin-ping_google\">\n<span class=\"sig-name descname\">django-admin ping_google [sitemap_url]</span><a class=\"heading-anchor\" href=\"#django-admin-ping_google\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Once the sitemaps application is added to your project, you may also\nping Google using the <code class=\"docutils literal notranslate\">ping_google</code> management command:</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>python manage<span class=\"o\">.</span>py ping_google <span class=\"p\">[</span><span class=\"o\">/</span>sitemap<span class=\"o\">.</span>xml<span class=\"p\">]</span>\n</code></pre></div>\n<dl class=\"django-admin-option\">\n<dt class=\"sig sig-object\" id=\"cmdoption-ping_google-sitemap-uses-http\">\n<span class=\"sig-name descname\">--sitemap-uses-http</span><span class=\"sig-prename descclassname\"></span><a class=\"heading-anchor\" href=\"#cmdoption-ping_google-sitemap-uses-http\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"version-note version-added\" data-version=\"2.2\" role=\"note\">\n<p class=\"version-note-title\">New in Django 2.2</p></aside>\n<p>Use this option if your sitemap uses <code class=\"docutils literal notranslate\">http</code> rather than <code class=\"docutils literal notranslate\">https</code>.</p>\n</section>\n</section>","rootId":"module-django.contrib.sitemaps","toc":[{"title":"概况","anchor":"overview","children":[]},{"title":"安装","anchor":"installation","children":[]},{"title":"Initialization","anchor":"initialization","children":[]},{"title":"Sitemap classes","anchor":"sitemap-classes","children":[]},{"title":"一个例子","anchor":"an-example","children":[]},{"title":"Sitemap class reference","anchor":"sitemap-class-reference","children":[]},{"title":"Shortcuts","anchor":"shortcuts","children":[{"title":"例如","anchor":"example","children":[]}]},{"title":"Sitemap for static views","anchor":"sitemap-for-static-views","children":[]},{"title":"Creating a sitemap index","anchor":"creating-a-sitemap-index","children":[]},{"title":"Template customization","anchor":"template-customization","children":[{"title":"Context variables","anchor":"context-variables","children":[]},{"title":"索引","anchor":"index","children":[]},{"title":"Sitemap","anchor":"id1","children":[]}]},{"title":"Pinging Google","anchor":"pinging-google","children":[{"title":"Pinging Google via manage.py","anchor":"pinging-google-via-manage-py","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API参考","url":"/zh-hans/3.0/ref/"},{"docname":"ref/contrib/index","title":"contrib packages","url":"/zh-hans/3.0/ref/contrib/"}],"prev":{"docname":"ref/contrib/redirects","title":"The redirects app","url":"/zh-hans/3.0/ref/contrib/redirects/"},"next":{"docname":"ref/contrib/sites","title":"The \"sites\" framework","url":"/zh-hans/3.0/ref/contrib/sites/"},"formats":{"html":"/zh-hans/3.0/ref/contrib/sitemaps/","markdown":"/zh-hans/3.0/ref/contrib/sitemaps.md","json":"/zh-hans/3.0/ref/contrib/sitemaps.json"},"source":"https://github.com/django/django/blob/stable/3.0.x/docs/ref/contrib/sitemaps.txt","official":"https://docs.djangoproject.com/zh-hans/3.0/ref/contrib/sitemaps/","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"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}