首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >‘'Account’对象没有属性'get_absolute_url‘

‘'Account’对象没有属性'get_absolute_url‘
EN

Stack Overflow用户
提问于 2011-02-20 20:15:47
回答 2查看 5.7K关注 0票数 4

当试图访问我的sitemap.xml时,我会收到以下错误:

代码语言:javascript
代码运行次数:0
运行
复制
'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

我在回溯中找不到这个“帐户”对象。我是不是没在这里进口什么东西?如果你需要更多的信息,请告诉我。

EN

回答 2

Stack Overflow用户

发布于 2016-03-25 01:03:08

你必须定义那个方法。

get_absolute_url

Model.get_absolute_url()

定义一个get_absolute_url()方法,告诉Django如何计算对象的规范URL。对于调用方,此方法应显示返回一个字符串,该字符串可用于通过HTTP引用对象。

例如:

代码语言:javascript
代码运行次数:0
运行
复制
def get_absolute_url(self):
    return "/people/%i/" % self.id

虽然这段代码是正确的和简单的,但它可能不是编写这种方法的最可移植的方法。相反()函数通常是最好的方法。

例如:

代码语言:javascript
代码运行次数:0
运行
复制
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/

票数 4
EN

Stack Overflow用户

发布于 2011-02-20 20: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中搜索它,因为显然有一篇文章在引用它。

代码语言:javascript
代码运行次数:0
运行
复制
   from django.contrib.contenttypes.models import ContentType
   ContentType.objects.filter(model__icontains="account")

你得到什么结果了吗?

更新:

所以你找到了一个'account' ContentType

现在您可以通过contenttype.model_class()获得这个类,从而找到它来实现get_absolute_url()那里的,因为听起来您并没有实际使用这个类,您可以通过查询ContentTypeArticle来找到哪个Article指向这个神秘的account ContentType。

代码语言:javascript
代码运行次数:0
运行
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5059729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档