You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

拍卖系统关闭出价按钮触发404错误,请求修复方案

拍卖系统404错误修复及功能实现方案

一、错误根源分析

当前点击「Close Offer」触发404的核心原因:

  • POST请求中错误使用request.GET.get获取参数,表单提交的参数在POST请求体内
  • 隐藏字段传递整个商品对象而非ID,无法正确查询对应List实例
  • 卖家判断逻辑错误,导致所有用户都能看到关闭按钮
  • 视图中未正确获取List模型实例,直接操作字符串变量引发查询失败

二、分步修复及功能实现

1. 修正商品详情页的关闭按钮逻辑(product_detail.html)

<ul>
    {% for offer in offers %}
        <li>{{ offer.user.username }} offered ${{ offer.offered_price }}</li>  
    {% endfor %}
    <!-- 仅卖家且拍卖未关闭时显示按钮 -->
    {% if user == product.seller and product.active_bool %}
        <form method="post" action="{% url 'off' %}">
            {% csrf_token %}
            <!-- 传递商品ID而非整个对象 -->
            <input type="hidden" name="product_id" value="{{ product.id }}">
            <button type="submit">Close Offer</button>
        </form>
    {% endif %}
    <!-- 拍卖关闭后状态提示 -->
    {% if not product.active_bool %}
        <p>This auction is closed. {% if product.winner == user %}You won this item!{% endif %}</p>
    {% endif %}
</ul>

改动点:

  • 修正卖家判断条件:{% if user == product.seller and product.active_bool %},严格控制按钮可见范围
  • 隐藏字段改为传递商品ID:name="product_id" value="{{ product.id }}",确保后端能正确定位商品
  • 添加拍卖关闭后的状态提示,赢家可看到专属提示信息

2. 修复off视图逻辑并实现关闭+指定赢家功能(views.py)

from django.shortcuts import get_object_or_404, redirect

@login_required(login_url="login")
def win(request, username):
    # 修正:通过winner外键过滤用户拍得的商品
    your_win = List.objects.filter(winner__username=username)
    return render(request, "auctions/win.html",{
         "user_winlist" : your_win,
    })

@login_required(login_url="login")
def off(request):
    # 从POST请求中获取商品ID
    product_id = request.POST.get('product_id')
    # 用get_object_or_404确保获取有效商品实例,避免无效ID引发404
    product = get_object_or_404(List, id=product_id)
    
    # 仅卖家可执行关闭操作
    if request.user == product.seller:
        # 标记拍卖为关闭状态
        product.active_bool = False
        # 找到该商品的最高出价者,设为赢家
        highest_bid = Bid.objects.filter(product=product).order_by('-offered_price').first()
        if highest_bid:
            product.winner = highest_bid.user
        product.save()
        # 跳转至赢家页面
        return redirect('win', username=request.user.username)
    
    # 非卖家访问则返回商品详情页
    return redirect('product_detail', product_id=product.id)

改动点:

  • 用request.POST.get获取表单参数,匹配POST请求方式
  • 用get_object_or_404获取List实例,自动处理无效ID的404场景
  • 修正卖家判断逻辑:request.user == product.seller,严格校验操作权限
  • 关闭拍卖时设置product.active_bool = False(对应模型中的状态字段)
  • 添加赢家指定逻辑:取最高出价的用户设为商品winner
  • 用redirect替代直接调用视图函数,符合Django路由规范

3. 完善赢家页数据展示(win.html)

确保win.html继承layout布局并展示商品规格,示例如下:

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

{% block body %}
<h2>My Winnings</h2>
{% if user_winlist %}
    <div class="row">
        {% for item in user_winlist %}
            <div class="col-md-4 mb-4">
                <div class="card">
                    {% if item.image %}
                        <img src="{{ item.image.url }}" class="card-img-top" alt="{{ item.title }}">
                    {% endif %}
                    <div class="card-body">
                        <h5 class="card-title">{{ item.title }}</h5>
                        <p class="card-text">{{ item.description }}</p>
                        <p><strong>Category:</strong> {{ item.category }}</p>
                        <p><strong>Winning Price:</strong> ${{ item.first_bid }}</p>
                        <p><strong>Status:</strong> {{ item.get_status_display }}</p>
                        <!-- 可添加更多商品规格字段 -->
                    </div>
                </div>
            </div>
        {% endfor %}
    </div>
{% else %}
    <p>You haven't won any auctions yet.</p>
{% endif %}
{% endblock %}

改动点:

  • 继承layout.html布局,满足页面布局统一要求
  • 遍历user_winlist展示商品标题、描述、分类、成交价、状态等完整规格信息

4. 商品页禁止已关闭拍卖的出价功能

在商品详情页的出价表单处添加判断,仅当拍卖活跃时显示:

{% if product.active_bool %}
    <!-- 出价表单代码 -->
    <form method="post" action="{% url 'bid' %}">
        {% csrf_token %}
        <input type="hidden" name="product_id" value="{{ product.id }}">
        <input type="number" step="0.01" name="bid_price" placeholder="Enter your bid">
        <button type="submit">Place Bid</button>
    </form>
{% else %}
    <p>Bidding is closed for this item.</p>
{% endif %}

三、关键逻辑验证

  1. 仅卖家登录后能看到「Close Offer」按钮
  2. 点击关闭后,拍卖状态设为关闭,最高出价者自动被设为赢家
  3. 赢家可在「My Winnings」页面看到自己拍得的商品及完整规格
  4. 关闭后的商品无法再提交出价

内容的提问来源于stack exchange,提问作者zahra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 00:27:09