django 找不到带参数“(',)”的“”的反转,尝试了1个模式

mrwjdhj3  于 2023-02-25  发布在  Go
关注(0)|答案(1)|浏览(153)

每当我在登录页面上单击此提交按钮时,都会收到此错误消息。我已经查看了触发此类错误的典型位置,但我找不到此处的错误之处。

    • 错误:**
Reverse for 'listing' with arguments '('',)' not found. 1 pattern(s) tried: ['listing/(?P<id>[0-9]+)$']

在页面的更下方,在回溯中这一行被突出显示如下。2为了清晰起见,它突出显示了该视图中的第二个返回渲染。

auctions\views.py, line 229, in closeListing
            return render(request, "auctions/closeListing.html", {

我知道在我的视图和网址中我传递了id参数,但是在模板中我传递了www.example.com参数,但是由于某种原因,这在我的项目中一直有效,但是在这里不起作用.我实际上只是试图将视图和网址中的所有参数更改为listing_id,以匹配模板中的listing.id,但这产生了完全相同的错误,所以我真的很困惑.listing.id but this has been working throughout my project for some reason but just not working here. I actually just tried to change all the arguments in views and urls to listing_id to match the listing.id in the template and this produced the exact same error so I'm really lost.

    • 查看次数. 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
        })
    • 网址. py**
path("listing/<int:id>/close", views.closeListing, name="closelisting")
    • 列表页面. 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 %}
0dxa2lsx

0dxa2lsx1#

当我得到这个错误,我正在建立一个类似的拍卖网站。问题是,我试图扩展前一页,而且,我使用了参数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更改为#,它工作了。当然#是一个测试。我需要在模板的上下文中添加拍卖对象。

相关问题