“帐户”对象没有属性“get_absolute_url”;
当尝试访问我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须定义该方法。
get_absolute_url
Model.get_absolute_url()
定义 get_absolute_url() 方法来告诉 Django 如何计算对象的规范 URL。对于调用者来说,此方法应该返回一个可用于通过 HTTP 引用对象的字符串。
例如:
虽然这段代码是正确且简单的,但它可能不是编写这种方法的最可移植的方式。 verse() 函数通常是最好的方法。
例如:
参考: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:
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:
Reference: https://docs.djangoproject.com/en/1.9/ref/models/instances/
我从未使用过它或这个 wiki 应用程序,但听起来您的
Account
模型没有get_absolute_url
方法。http://docs.djangoproject。 com/en/dev/ref/contrib/sitemaps/#django.contrib.sitemaps.Sitemap.location
你在使用这个应用程序吗? http://code.google .com/p/django-wikiapp/source/browse/trunk/wiki/models.py?r=161 (我刚刚搜索了你的回溯来找到它)
Group
是一个通用的外国键,以便它可以指向您的任何模型,这意味着它指向的每个模型都必须定义一个get_absolute_url
。更新:
如果您没有
Account
模型,我建议在django.contrib.contenttypes.ContentType
中搜索它,因为显然一篇文章正在引用它..你得到任何结果吗?
更新:
因此您已找到
'account'
ContentType
。现在您可以通过
contenttype.model_class()
获取该类,从而找到它来实现get_absolute_url()
那里或,因为它听起来像您'如果您实际上并未使用此类,您可以通过通过ContentType 查询
。Article
来找到哪些Article
指向这个神秘的account
ContentTypeI've never used it, or this wiki app, but it simply sounds like your
Account
model doesn't have aget_absolute_url
method.http://docs.djangoproject.com/en/dev/ref/contrib/sitemaps/#django.contrib.sitemaps.Sitemap.location
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 aget_absolute_url
defined.Update:
If you don't have an
Account
model, I'd suggest searching for it indjango.contrib.contenttypes.ContentType
because apparently an article is referencing it..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 implementget_absolute_url()
there or since it sounds like you're not actually using this class, you can find whichArticle
's are pointing to this mysteriousaccount
ContentType by queryingArticle
byContentType
.