【发布时间】:2011-04-16 17:28:50
【问题描述】:
我正在使用 get_absolute_url 方法获取动态查询的 url,但是当显示链接时,它只显示 get_absolute_url 方法中的第一个参数而不是第二个参数。只有当我使用模型的 ForeignKey 作为第一个参数时,它才会这样做。下面是代码。
class Topic(models.Model):
topic_id = models.AutoField(primary_key=True)
forum_id = models.ForeignKey(Forum)
topic_title = models.CharField(max_length=400)
topic_date_time = models.DateTimeField(auto_now_add=True)
topic_user_id = models.IntegerField()
topic_views = models.IntegerField(default=0)
topic_replies = models.IntegerField(default=0)
topic_is_locked = models.BooleanField(default=False)
topic_is_sticky = models.BooleanField(default=False)
def __unicode__(self):
return '%s' % _(u'self.topic_title')
def get_absolute_url(self):
**return '/forums/%i/%i/' % (self.forum_id, self.topic_id)**
我该如何解决这个问题?谢谢!
【问题讨论】:
-
从
topic_instance.get_absolute_url()返回的字符串到底是什么? -
能否也显示 urls.py 的相应部分?虽然使用永久链接装饰器来反转您的网址可能会更好(docs.djangoproject.com/en/dev/ref/models/instances/…)
-
我找到了我的问题的答案,但肯定也会对此进行调查。谢谢!