【问题标题】:Django: Reverse for ‘topic_posts’ with arguments ‘(3, ”)’ not foundDjango:找不到带有参数'(3,'')'的'topic_posts'的反向
【发布时间】:2021-09-07 22:07:54
【问题描述】:

“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.idpost.topic.id 更改为整数值时,例如 1 和 24,网络将呈现。我评论了&lt;a href="{% url 'topic_posts' board.id post.topic.id %}"&gt;并添加了{{ board.id }} {{ post.topic.id }},看看查询是否有错误,结果没有问题,它会在网页上呈现1、24。然后我试了&lt;a href="{% url 'topic_posts' board.id 24 %}"&gt;,效果很好,但是&lt;a href="{% url 'topic_posts' board.id post.topic.id %}"&gt;还是不行。

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()

感谢阅读!

【问题讨论】:

  • 模板代码是否在for循环中?似乎当board.id=3 然后post.topic.id 是空的。这就是错误(3, '') 告诉您的内容。你能分享get_last_post的代码吗?这很可能没有返回任何东西。
  • 您好,感谢您的阅读。我已经编辑了帖子。当我评论 &lt;a href="{% url 'topic_posts' board.id post.topic.id %}"&gt;&lt;/a&gt; 时,{{ post.topic.id }} 会给我 24 分
  • 您的模板代码是否已在 for 循环中显示?当board.id3 时,我认为您不会为{{ post.topic.id }} 获得24。您可以通过在返回前在get_last_post 中打印print(Post.objects.filter(topic__board_id=3).order_by('-created_at').first()) 来检查这一点。这可能是空的。
  • 这个 id - {{ board.id }} {{ post.topic.id }} 给了我 id - 1 24,这与错误“(3,”)相矛盾,错误 board.id gives 3 ?
  • 是的,它在 for 循环中

标签: django django-urls django-url-reverse


【解决方案1】:
<a href="{% url 'topic_posts' board.id post.topic.id %}">

Django 无法理解这里的 boardpost。 尝试在返回渲染期间通过上下文与模板一起发送它

试试这个:

return render(request, 'topic_posts.html', {'topic': topic, 'board': board, 'post': post})

【讨论】:

  • 无论如何,谢谢你的回答。问题是for循环。我发布的 HTML 代码位于 for 循环中。我没有为 board.id = 3 填充任何数据,所以 post.topic.id 为空。
  • 谢谢@WeiWong。很高兴知道您的问题已解决。
【解决方案2】:

您的模板/HTML 代码中有一个循环。它适用于124 的原因是因为这是循环的第一次迭代,并且您的函数get_last_post 返回带有id1board 的最新帖子。但是,当 for 循环找到 board3id 时,不存在最新帖子并且返回 None 或为空。这就是您收到此错误的原因。错误本质上是说,我找不到只有board_id=3 和空post_id 的URL。

您可以通过打印来确认:

print(Post.objects.filter(topic__board_id=3).order_by('-created_at').first())

或者通过在您的数据库中检查具有id3 的板上的任何帖子。

您可以通过显示链接来解决此问题如果存在具有最新帖子的板,例如:

{% if post.topic.id %}
<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>
{% endif %}

【讨论】:

  • 太棒了!感谢您的时间!是的,我没有在 board.id = 3 上填充任何数据。现在我明白为什么它不起作用了。但是board.id = 3没有数据时如何避免问题
  • 您只需要确保在此为空时进行处理,就像我在 HTML 代码中显示的 if 语句一样。
  • 你太棒了!
猜你喜欢
  • 1970-01-01
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
  • 2021-11-09
  • 2021-03-29
  • 2019-09-14
  • 1970-01-01
相关资源
最近更新 更多