Django Sitemaps and “normal” views

后端 未结 2 1314
长发绾君心
长发绾君心 2021-01-30 11:34

Maybe I didn\'t understand the purpose of Sitemaps or maybe I didn\'t understand how to use sitemaps. Right now my sitemap is including all \"dynamically\" created pages, like t

2条回答
  •  天涯浪人
    2021-01-30 12:21

    I deal with it in this way:

    An abstract class for defining static page's attributes.

    class AbstractSitemapClass():
        changefreq = 'daily'
        url = None
        def get_absolute_url(self):
            return self.url
    

    The sitemap class for static pages:

    class StaticSitemap(Sitemap):
        pages = {
                 'home':'/', #Add more static pages here like this 'example':'url_of_example',
                 'contact':'/contact/',
                 }
        main_sitemaps = []
        for page in pages.keys():
            sitemap_class = AbstractSitemapClass()
            sitemap_class.url = pages[page]        
            main_sitemaps.append(sitemap_class)
    
        def items(self):
            return self.main_sitemaps    
        lastmod = datetime.datetime(2010, 8, 31)   #Enter the year,month, date you want in yout static page sitemap.
        priority = 1
        changefreq = "yearly"   
    

    Use this in the sitemaps dictionary to be used in urls.py:

    sitemaps = {
            'main':StaticSitemap,
            'flatpages':MyFlatPageSitemap,
            'model':PostSitemap,
            }
    

提交回复
热议问题