使用Django设置站点地图

时间:2011-07-28 20:31:52

标签: django django-urls

我在使用站点地图时遇到了一些问题。

urls.py

from django.contrib import sitemaps
from oportunidade.views import OportunidadeSitemap
sitemaps = {'oportunidade': OportunidadeSitemap}
...
    url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),

views.py

...
class OportunidadeSitemap(Sitemap):
    changefreq = "never"
    priority = 0.5

    def items(self):
        return Oportunidade.objects.filter(ativo=True)

    def lastmod(self, obj):
        return obj.ultima_alteracao 

但是当我访问http://localhost:8000/sitemap.xml时出现以下错误 'Oportunidade'对象没有属性'get_absolute_url'

这是我的“Oportunidade”模型:

class Oportunidade(models.Model):

    user = models.ForeignKey(User)    

    titulo = models.CharField('Titulo',max_length=31)

 ...
    def __unicode__(self):
        return self.titulo

我很困惑如何设置站点地图。

2 个答案:

答案 0 :(得分:1)

根据文档:https://docs.djangoproject.com/en/1.3/ref/contrib/sitemaps/#sitemap-class-reference

如果您没有为sitemap类提供位置,它将在每个对象上查找get_absolute_url。

因此,您需要在sitemap类上指定location属性,或者在对象上指定get_absolute_url。这应该让你去。

答案 1 :(得分:1)

请查看Django's sitemap class reference。虽然您实施了必要的items方法,但您似乎缺少站点地图中的location方法(或属性)或模型类中的get_absolute_url方法。

  

如果未提供location,框架将在items()返回的每个对象上调用get_absolute_url()方法。

最简单的方法是在get_absolute_url()模型类中实施Oportunidade

相关问题