【问题标题】:reverse for ' with arguments '()' and keyword arguments not found. 1 pattern(s) tried:使用参数 '()' 和未找到关键字参数的 ' 反向。尝试了 1 种模式:
【发布时间】:2016-12-11 20:24:34
【问题描述】:

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

未找到带有参数“()”和关键字参数“{'bid_id': 16}”的“accept_bid”的反向操作。尝试了 1 种模式:['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 %}

【问题讨论】:

    标签: django django-models django-views django-urls django-generic-views


    【解决方案1】:

    您还需要将值包含到post_id

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

    【讨论】:

    • 我从我的投标模型中定义的 post 字段 (ForeignKey) 中获取 post_id。如何在获取 post_id 的答案中定义whatever_post_id_is??
    猜你喜欢
    • 2015-02-08
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2019-04-09
    • 2016-02-07
    • 2016-11-17
    • 2023-03-04
    相关资源
    最近更新 更多