如何将首页添加到Django网站地图?

Mar*_*rty 1 python sitemap django django-sitemaps

给定sitemap类的sitemap在该位置生成一个sitemap, example.com/sitemap.xml

从django.contrib.sitemaps导入从blog.models导入Sitemap到给定Sitemap类的Entry,

class BlogSitemap(Sitemap):
    changefreq = "never"
    priority = 0.5

    def items(self):
        return Entry.objects.filter(is_draft=False)

    def lastmod(self, obj):
        return obj.pub_date
Run Code Online (Sandbox Code Playgroud)

生成的站点地图包含Blog模型中的所有对象,但不包含Queryset之外的内容。如何将主页添加到站点地图中?

网址

from django.contrib.sitemaps.views import sitemap
from blog.sitemaps import BlogSitemap


sitemaps = {
    'blog': BlogSitemap
}
urlpatterns = [
    url(r'^$', 'blog.views.home'),
    url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},
        name='django.contrib.sitemaps.views.sitemap'),
]
Run Code Online (Sandbox Code Playgroud)

Ala*_*air 5

为静态视图创建站点地图

class StaticViewSitemap(sitemaps.Sitemap):
    priority = 0.5
    changefreq = 'daily'

    def items(self):
        return ['home']

    def location(self, item):
        return reverse(item)
Run Code Online (Sandbox Code Playgroud)

假设您的首页网址格式为“ home”

url(r'^$', views.homepage, name="home"),
Run Code Online (Sandbox Code Playgroud)

然后将url 添加StaticViewSitemapsitemaps您的urls.py中。

sitemaps = {
    'blog': BlogSitemap,
    'static': StaticViewSiteMap,
}
Run Code Online (Sandbox Code Playgroud)