Django Sitemaps and “normal” views

余生颓废 提交于 2019-12-03 03:13:35

问题


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 the blog posts. But how do I add "static" pages like my index and contact page? Or shouldn't these views be in the sitemap? I thought -all- pages should be on the sitemap. For example, how would I include such view

(r'^contact/', include('contact-recaptcha.urls')),

if there is no queryset?

For reference: http://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/

Thanks in advance!


回答1:


Another simpler alternative:

from django.core.urlresolvers import reverse
from django.contrib.sitemaps import Sitemap


class ViewSitemap(Sitemap):
    """Reverse 'static' views for XML sitemap."""

    def items(self):
        # Return list of url names for views to include in sitemap
        return ['homepage', 'news_article_list', 'contact_page']

    def location(self, item):
        return reverse(item)


sitemaps = {'views': ViewSitemap}

I've deliberately omitted lastmod and changefreq, as specifying incorrect/assumed data is worse than not including it.




回答2:


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,
        }


来源:https://stackoverflow.com/questions/4836188/django-sitemaps-and-normal-views

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!