''topic_posts' 的反面在其他地方也能正常工作。但它在这里不起作用。HTML 文件
<td class="align-middle">
{% with post=board.get_last_post %}
<small>
<a href="{% url 'topic_posts' board.id post.topic.id %}">
By {{ post.created_by.username }} at {{ post.created_at }}
id - {{ board.id }} {{ post.topic.id }}
</a>
</small>
{% endwith %}
</td>
网址设置
path('boards/<int:board_id>/topics/<int:topic_id>/', views.topic_posts, name='topic_posts'),
topic = get_object_or_404(Topic, board_id=board_id, id=topic_id)
return render(request, 'topic_posts.html', {'topic': topic})
当我将board.id
and更改post.topic.id
为 integer value 时,例如 1 和 24,web 将呈现。我评论<a href="{% url 'topic_posts' board.id post.topic.id %}">
添加{{ board.id }} {{ post.topic.id }}
,看看查询是否有错误,结果没有问题,它会在网页上呈现 1, 24。然后我尝试<a href="{% url 'topic_posts' board.id 24 %}">
了它工作正常,但<a href="{% url 'topic_posts' board.id post.topic.id %}">
仍然无法工作。
class Board(models.Model):
name = models.CharField(max_length=30, unique=True)
description = models.CharField(max_length=100)
def __str__(self):
return self.name
def get_posts_count(self):
return Post.objects.filter(topic__board=self).count()
def get_last_post(self):
return Post.objects.filter(topic__board=self).order_by('-created_at').first()
谢谢阅读!