Get_next_by_FOO方法错误地返回链接



我的问题是我指定了get_next_by_FOO方法,但是链接不能正常工作。

# models.py

class Project(models.Model):
user = ForeignKey(settings.AUTH_USER_MODEL, on_delete=CASCADE, db_index=True)
name = CharField(max_length=150, db_index=True)
cover = models.ImageField( upload_to="project_photos/", null=True, blank=True
)
site = URLField( max_length=200)
description = TextField()
note = CharField( max_length=150)
created_at = DateTimeField(auto_now_add=True)

def next(self):
return self.get_next_by_created_at()
def pre(self):
return self.get_previous_by_created_at()

# html

<a href="{{ project.next }}">Next project</a>
<a href="{{ project.pre }}">Previous project</a>

并给出以下链接:

http://localhost: 8000/项目/8/% D0 % 9 d % D0%BE % D0%B2%D0%B0%D1 d1 % % 8 f % 20% 80% d1 d0%b1%d1 % % 83% 80% d0%b8%d0%ba % D0%B0

模型的id没有改变,下一个或上一个模型实例的名称被附加到链接的末尾

<a href="{% url 'project_detail_url' project.next.pk %}">Next project</a>
<a href="{% url 'project_detail_url' project.pre.pk %}">Previous project</a>

相关内容

  • 没有找到相关文章

最新更新