我正在尝试获取一个ListView,我的模板中定义了get_absolute_url。但它引发了一个错误:
Reverse for 'accept_bid' with arguments '()' and keyword arguments '{'bid_id': 16}' not found. 1 pattern(s) tried: ['post/(?P<post_id>[\\w-]+)/bid/(?P<bid_id>[\\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 %}
答案 0 :(得分:4)
您还需要将值包含到post_id
:
kwargs={"bid_id": self.id, "post_id": self.post.id}