每当我点击列表页面上的此提交按钮时,都会收到此错误。我已经查看了触发此类错误的典型位置,但我看不出我在这里做错了什么。
错误:
Reverse for 'listing' with arguments '('',)' not found. 1 pattern(s) tried: ['listing/(?P<id>[0-9]+)$']
在页面的更下方,在回溯中,该行突出显示,如下所示。为了清楚起见,它突出显示了该视图中的第二个返回渲染。
auctions\views.py, line 229, in closeListing
return render(request, "auctions/closeListing.html", {
我知道在我的视图和网址中我传递了 id 参数,但随后在模板上我传递了 Listing.id 参数,但这由于某种原因在我的项目中一直有效,但在这里不起作用。实际上,我只是尝试将视图和 url 中的所有参数更改为listing_id,以匹配模板中的listing.id,这产生了完全相同的错误,所以我真的迷路了。
views.py
def closeListing(request, id):
bids = Bid.objects.filter(bid_item_id=id).order_by('-bid_input')
winner = Bid.objects.filter(bid_item_id=id).latest('bid_input').bidder.id
winner_name = Bid.objects.filter(bid_item_id=id).latest('bid_input').bidder.username
currentHighest = Bid.objects.filter(bid_item_id=id).aggregate(Max('bid_input'))['bid_input__max']
if request.method == "POST":
if bids == 0:
return render(request, "auctions/closeListing.html", {
"bids": bids,
"error": "No bids have been placed on your listing",
})
else:
Listing.objects.filter(id=id).update(status='closed')
Listing.objects.filter(id=id).update(winner=winner)
closed = True
return render(request, "auctions/closeListing.html", {
"bids": bids,
"current": currentHighest,
"winner": winner_name,
"closed": True
})
else:
return render(request, "auctions/closeListing.html", {
"bids": bids
})
urls.py
path("listing/<int:id>/close", views.closeListing, name="closelisting")
listingPage.html
{% if request.user.is_authenticated and request.user == listing.lister %}
<div>
<form action="{% url 'closelisting' listing.id %}" method="post">
{% csrf_token %}
<input type=submit value="Close Listing">
</form>
</div>
{% endif %}
当我收到此错误时,我正在构建一个类似的拍卖网站。问题是我试图扩展上一页,而且我在没有创建拍卖对象的情况下使用了参数
auctions.id
。
我的代码如下所示:
{% extends 'auctions/listing.html'%}
{% block bids %}
{% if message %} {{message}} {% endif %}
<h4> Top bid: ${{top}}</h4>
<form action="{% url 'bids' auction_id %}">
{% csrf_token %}
<label for="bid"> Your Bid: </label>
<input type="number" name="my_bid">
<input type="submit" value="Place Bid">
</form>
{% endblock %}
通过删除第一行,并将表单操作 URL 更改为
#
,它起作用了。当然#
是一个考验。我需要在模板的上下文中添加拍卖对象。