“帐户”对象没有属性“get_absolute_url”;

发布于 2024-10-18 10:32:33 字数 392 浏览 3 评论 0原文

当尝试访问我的 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

我在回溯中找不到此“帐户”对象。我在这里导入东西失败了吗?如果您需要更多信息,请告诉我。

When attempting to access my sitemap.xml, I'm receiving this error:

'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

I can't find this 'Account' object in the traceback. Am I failing to import something here? Please let me know if you need more information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2024-10-25 10:32:33

您必须定义该方法。


get_absolute_url

Model.get_absolute_url()

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

例如:

def get_absolute_url(self):
    return "/people/%i/" % self.id

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

例如:

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/模型/实例/

You have to define that method.


get_absolute_url

Model.get_absolute_url()

Define a get_absolute_url() method to tell Django how to calculate the canonical URL for an object. To callers, this method should appear to return a string that can be used to refer to the object over HTTP.

For example:

def get_absolute_url(self):
    return "/people/%i/" % self.id

While this code is correct and simple, it may not be the most portable way to write this kind of method. The reverse() function is usually the best approach.

For example:

def get_absolute_url(self):
    from django.core.urlresolvers import reverse
    return reverse('people.views.details', args=[str(self.id)])

Reference: https://docs.djangoproject.com/en/1.9/ref/models/instances/

允世 2024-10-25 10:32:33

我从未使用过它或这个 wiki 应用程序,但听起来您的 Account 模型没有 get_absolute_url 方法。

http://docs.djangoproject。 com/en/dev/ref/contrib/sitemaps/#django.contrib.sitemaps.Sitemap.location

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

你在使用这个应用程序吗? 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

I've never used it, or this wiki app, but it simply sounds like your Account model doesn't have a get_absolute_url method.

http://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/#django.contrib.sitemaps.Sitemap.location

If location isn't provided, the
framework will call the
get_absolute_url() method on each
object as returned by items().

Are you using this app? http://code.google.com/p/django-wikiapp/source/browse/trunk/wiki/models.py?r=161 (I just searched your traceback to find it)

Group is a generic foreign key so it can point to any of your models, which means every model it points to must have a get_absolute_url defined.

Update:

If you don't have an Account model, I'd suggest searching for it in django.contrib.contenttypes.ContentType because apparently an article is referencing it..

   from django.contrib.contenttypes.models import ContentType
   ContentType.objects.filter(model__icontains="account")

Do you get any results?

Update:

So you've found an 'account' ContentType.

Now you can get the class via contenttype.model_class() and thus find it to implement get_absolute_url() there or since it sounds like you're not actually using this class, you can find which Article's are pointing to this mysterious account ContentType by querying Article by 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文