对带有参数“()”且未找到关键字参数的“进行反转”。尝试了 1 种模式:

reverse for ' with arguments '()' and keyword arguments not found. 1 pattern(s) tried:

我正在尝试获取在我的模板中定义了 get_absolute_url 的 ListView。但它引发了一个错误:

Reverse for 'accept_bid' with arguments '()' and keyword arguments '{'bid_id': 16}' not found. 1 pattern(s) tried: ['post/(?P[\w-]+)/bid/(?P[\w-]+)/$'].

我是否需要在我的视图中定义这两个整数 ID 还是存在其他问题?

我很乐意帮助我解决这个问题。

models.py:

class Bid(models.Model):

    post = models.ForeignKey(Post, related_name = "bids")
    user = models.OneToOneField(User, null=True, blank=True)
    amount = models.IntegerField()

    def get_absolute_url(self):
        return reverse("accept_bid", kwargs={"bid_id": self.id})

Views.py:

class LiveBids(LoginRequiredMixin, ListView, FormView ):
    template_name = 'live_bids.html'
    def get_queryset(self):

         return Post.objects.all().prefetch_related('bids').filter(user=self.request.user).order_by('id')

urls.py:

url(r'^live_bids/$', LiveBids.as_view(model=Post), name='LiveBids'),
url(r'^post/(?P<post_id>[\w-]+)/bid/(?P<bid_id>[\w-]+)/$', views.accept_bid, name='accept_bid'),

live_bids.html:

{% for bid in post.bids.all %}
    {{bid.amount}}
    <p><a href='{{ bid.get_absolute_url }}'>Accept</p>
{% endfor %}

您还需要包括 post_id 的值:

kwargs={"bid_id": self.id, "post_id": self.post.id}