当试图访问我的sitemap.xml时,我会收到以下错误:
'Account' object has no attribute 'get_absolute_url' on line 112.
109. def get_absolute_url(self):
110. if self.group is None:
111. return reverse('wiki_article', args=(self.title,))
112. return self.group.get_absolute_url() + 'wiki/' + self.title
我在回溯中找不到这个“帐户”对象。我是不是没在这里进口什么东西?如果你需要更多的信息,请告诉我。
发布于 2016-03-24 17:03:08
你必须定义那个方法。
get_absolute_url
Model.get_absolute_url()
定义一个get_absolute_url()方法,告诉Django如何计算对象的规范URL。对于调用方,此方法应显示返回一个字符串,该字符串可用于通过HTTP引用对象。
例如:
def get_absolute_url(self):
return "/people/%i/" % self.id
虽然这段代码是正确的和简单的,但它可能不是编写这种方法的最可移植的方法。相反()函数通常是最好的方法。
例如:
def get_absolute_url(self):
from django.core.urlresolvers import reverse
return reverse('people.views.details', args=[str(self.id)])
参考资料:https://docs.djangoproject.com/en/1.9/ref/models/instances/
发布于 2011-02-20 12:29:18
我从未使用过它,也从未使用过这个wiki应用程序,但它听起来就像您的Account
模型没有get_absolute_url
方法。
http://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/#django.contrib.sitemaps.Sitemap.location
如果没有提供位置,框架将对每个对象调用由items()返回的get_absolute_url()方法。
你在使用这个应用程序吗?http://code.google.com/p/django-wikiapp/source/browse/trunk/wiki/models.py?r=161 (我只是搜索了你的回溯以找到它)
Group
是一个通用外键,因此它可以指向您的任何模型,这意味着它所指向的每个模型都必须定义一个get_absolute_url
。
更新:
如果您没有Account
模型,我建议在django.contrib.contenttypes.ContentType
中搜索它,因为显然有一篇文章在引用它。
from django.contrib.contenttypes.models import ContentType
ContentType.objects.filter(model__icontains="account")
你得到什么结果了吗?
更新:
所以你找到了一个'account'
ContentType
。
现在您可以通过contenttype.model_class()
获得这个类,从而找到它来实现get_absolute_url()
那里的或,因为听起来您并没有实际使用这个类,您可以通过查询ContentType
的Article
来找到哪个Article
指向这个神秘的account
ContentType。
content_type = ContentType.objects.get(model__icontains='account')
articles_pointing_to_account = Article.objects.filter(content_type__pk=content_type.pk)
# Now it's your choice what to do with these articles.
# I'd be curious what they are and how they managed to pull off this stunt
# before removing their generic relation to Account
https://stackoverflow.com/questions/5059729
复制