I am trying to get a ListView for which get_absolute_url
is defined in my template. But it raises an error:
Reverse for 'accept_bid' with arguments '()' and keyword arguments '{'bid_id': 16}' not found. 1 pattern(s) tried: ['post/(?P[\w-]+)/bid/(?P[\w-]+)/$'].
Do I need to define the the two integer id's in my view or is there some other problem?
I would appreciate helping me in solve this.
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 %}
You need to include also the value to post_id
:
kwargs={"bid_id": self.id, "post_id": self.post.id}