使用 Django 设置站点地图
我在使用站点地图时遇到一些问题。
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
我很困惑如何设置站点地图。
I am having some problems with sitemaps.
urls.py
from django.contrib import sitemaps
from oportunidade.views import OportunidadeSitemap
sitemaps = {'oportunidade': OportunidadeSitemap}
...
url(r'^sitemap\.xml
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
But I get the following error when I access http://localhost:8000/sitemap.xml
'Oportunidade' object has no attribute 'get_absolute_url'
Here is my "Oportunidade" model:
class Oportunidade(models.Model):
user = models.ForeignKey(User)
titulo = models.CharField('Titulo',max_length=31)
...
def __unicode__(self):
return self.titulo
I am very confused how to set sitemap.
, 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
views.py
But I get the following error when I access http://localhost:8000/sitemap.xml
'Oportunidade' object has no attribute 'get_absolute_url'
Here is my "Oportunidade" model:
I am very confused how to set sitemap.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请查看 Django 的站点地图类参考。尽管您实现了必要的
items
方法,但您似乎缺少站点地图中的location
方法(或属性)或get_absolute_url
模型类中的方法。最简单的方法是在
Oportunidade
模型类中实现get_absolute_url()
。Please take a look at Django's sitemap class reference. Although you implement the necessary
items
method, you seem to be missing either thelocation
method (or attribute) in your sitemap or theget_absolute_url
method in you model class.Easiest way to go forward would be to implement
get_absolute_url()
in youOportunidade
model class.根据文档: https://docs.djangoproject .com/en/1.3/ref/contrib/sitemaps/#sitemap-class-reference
如果您没有为站点地图类提供位置,它将在每个站点上查找 get_absolute_url 目的。
因此,您需要在站点地图类上指定位置属性,或者在对象上指定 get_absolute_url。这应该能让你继续前进。
According to the docs: https://docs.djangoproject.com/en/1.3/ref/contrib/sitemaps/#sitemap-class-reference
If you don't provide a location for the sitemap class, it will look for get_absolute_url on each object.
So, you'll either need to specify a location property on your sitemap class, or get_absolute_url on your object. That should get you going.