错误“列表匹配查询不存在”的原因。

The cause of the error "List matching query does not exist."

提问人:zahra shokrizadeh 提问时间:8/14/2023 最后编辑:zahra shokrizadeh 更新时间:8/15/2023 访问量:50

问:

我正在编写一个 Django 拍卖程序,在程序的最后几关,当我编写代码来选择拍卖的获胜者并使用 sqlite 创建相应的表时,我遇到了这个错误。此错误后,主页上将不显示任何产品,也无法看到产品信息。 此错误的原因是什么?

我更改了表的列的名称,但它没有效果,其余代码在创建此表之前正在工作。

Traceback (most recent call last):
  File "C:\Users\N\AppData\Roaming\Python\Python311\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\N\AppData\Roaming\Python\Python311\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\N\Desktop\New folder\commerce\auctions\views.py", line 72, in item_detail
    bid_desc = List.objects.get(pk = bidid, active_bool = True)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\N\AppData\Roaming\Python\Python311\site-packages\django\db\models\manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\N\AppData\Roaming\Python\Python311\site-packages\django\db\models\query.py", line 637, in get
    raise self.model.DoesNotExist(
    ^

Exception Type: DoesNotExist at /auctions/1
Exception Value: List matching query does not exist.


这些代码与显示和模板中的获胜部分相关。

# item detail page
def item_detail(request, bidid):
    bid_desc = List.objects.get(pk = bidid, active_bool = True)
    nowbids = Bidmodel.objects.filter(listb_id = bidid)
    
    return render(request, "auctions/item_detail.html",{
        "list": bid_desc,
        "comments" : Comments.objects.filter(listc_id = bidid),
        "now_bid": minbid(bid_desc.first_bid, nowbids),
})

# winner
def winner(request):
    bid_id = request.GET["listid"]
    nowbids = Bidmodel.objects.filter(listb_id = bid_id)
    biddesc = List.objects.get(pk = bid_id, active_bool = True)
    maxbid = minbid(biddesc.first_bid, nowbids)

    try:
        winner_who = Bidmodel.objects.get(bid = maxbid, listb_id = bid_id)
        winner_list = List.objects.get(id = bid_id)
        win = Winner(win_list = winner_list, user = winner_who.user)
        winners_name = winner_who.user
    
    except:
        winner_list = List.objects.get(first_bid = maxbid, id = bid_id)
        win = Winner(win_list = winner_list, user = winner_list.user)
        winners_name = winner_list.user

    biddesc.active_bool = False
    biddesc.save()
    win.save()
    messages.success(request, f"{winners_name} won {win.win_list.title}.")
    return redirect("index")



# check winner
def winner_check(request):
    try:
        yourwin = Winner.objects.filter(user = request.user.username)
    except:
        yourwin = None

    return render(request, "auctions/winner_check.html",{
        "user_winlist" : yourwin,
    })



{% extends "auctions/layout.html" %}

{% block body %}
    <h2>Your Winnings</h2>
    <div>
        {% for list in user_winlist %}
            <div>
                <a>{{list.bid_win_list.title}}</a>
                <a>{{list.bid_win_list.category}}</a>
                <img src="{{ list.bid_win_list.image_url }}">
                <p>{{ list.bid_win_list.descRIPTION }}</p>
                <div>This bid was listed by <a>{{ list.bid_win_list.user }}</a>.</div>
            </div>  
        {% endfor %}
    </div>


CREATE TABLE auctions_winner(
id INTEGER PRIMARY KEY AUTOINCREMENT,
user TEXT,
win_list_id INTEGER NOT NULL
);
python html sql django sqlite

评论

0赞 Michael Ruth 8/14/2023
请提供一个最小的可重复示例。如果没有代码,我最好的猜测是,这是对 id=1 的拍卖表中记录的 GET 请求,并且不存在此类记录。/auctions/1
0赞 zahra 8/14/2023
@MichaelRuth 非常感谢您的回复 在创建表格之前,/auctions/1 显示产品规格,但现在不显示。代码量比较大,为了更好的理解,需要发送整个程序,这是不可能的......但是,在接下来的几分钟内,我将发布问题的编辑中,我还将编写与拍卖中奖部分相关的代码。
0赞 Anentropic 8/15/2023
错误来自视图中的一条线,该线当前未显示在问题中。数据库中肯定有列表吗?的值是否正确?item_detailactive_bool = Truebidid
0赞 zahra 8/15/2023
@Anentropic我在这个函数中有“active_bool = True”,如果我删除它,它会显示信息,但是,无法关闭拍卖并确定获胜者,并且它再次为我提到的函数提供相同的错误在问题中。
0赞 zahra 8/15/2023
@Anentropic 我在描述中添加了您提到的功能,感谢您的回复。

答: 暂无答案