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

Django拍卖系统权限控制、404错误及拍卖收尾问题咨询

Django拍卖应用问题修复与功能实现

1. 修复未登录用户出价触发404错误

问题根源:@login_required装饰器默认跳转路径是/accounts/login/,但项目URL配置中登录路径为/login,导致路径不匹配。

解决步骤:

  • 在项目的settings.py中添加配置,指定登录跳转URL:
LOGIN_URL = 'login'

这样未登录用户访问带@login_required的视图时,会跳转到/login?next=/build/1/,而非默认的/accounts/login/,匹配现有URL配置。

2. 实现未登录用户出价限制与提示

修改product_detail.html模板,针对未登录用户隐藏出价表单并显示登录提示:

{% if not product.winner %}
    {% if user.is_authenticated %}
        <form method="post" action="{% url 'build' product.id %}">
            {% csrf_token %}
            <label for="offered_price">Your Offer:</label>
            <input type="number" id="offered_price" name="offered_price" step="0.01" min="{{ product.first_bid }}" required>
            <button type="submit">Submit Offer</button>
        </form>
    {% else %}
        <p>请<a href="{% url 'login' %}?next={{ request.path }}">登录</a>后才能进行出价操作。</p>
    {% endif %}
{% endif %}
  • 注:模板中原min="{{ product.price }}"为错误写法,需改为min="{{ product.first_bid }}",对应模型中的起始出价字段。

同时可删除build视图内的if user.is_authenticated判断,因为@login_required已确保只有登录用户能进入该视图。

3. 实现关闭拍卖并指定买家功能

现有代码已基本适配该需求,只需完善以下内容:

模型层面

List模型已具备必要字段:

  • winner:存储中标用户
  • active_bool:标记拍卖是否处于激活状态

视图层面

修改accept视图,添加关闭拍卖的逻辑:

@login_required
def accept(request, product_id, bid_id):
    product = get_object_or_404(List, pk=product_id)
    offer = get_object_or_404(Bid, pk=bid_id)
    if request.user == product.seller:
        product.winner = offer.user
        product.active_bool = False  # 关闭拍卖
        product.save()
        messages.success(request, f"The product has been sold to {offer.user.username} for ${offer.offered_price}.")
    else:
        messages.warning(request, "You're not authorized to perform this action.")
    return redirect('product_detail', product_id)

模板层面

在product_detail.html中添加卖家专属的拍卖关闭入口,并优化状态显示:

<!-- 卖家显示中标选择按钮 -->
{% if user == product.seller and product.active_bool %}
    {% if offers %}
        <h3>选择中标买家:</h3>
        {% for offer in offers %}
            <a href="{% url 'accept' product.id offer.id %}">接受{{ offer.user.username }}的出价${{ offer.offered_price }}</a>
        {% endfor %}
    {% else %}
        <p>暂无出价,无法关闭拍卖。</p>
    {% endif %}
{% endif %}

<!-- 已关闭拍卖的状态提示 -->
{% if not product.active_bool %}
    <p>此拍卖已结束,中标者:{{ product.winner.username }}</p>
{% endif %}

<!-- 出价表单仅在拍卖激活且用户登录时显示 -->
{% if product.active_bool and not product.winner %}
    {% if user.is_authenticated %}
        <form method="post" action="{% url 'build' product.id %}">
            {% csrf_token %}
            <label for="offered_price">Your Offer:</label>
            <input type="number" id="offered_price" name="offered_price" step="0.01" min="{{ product.first_bid }}" required>
            <button type="submit">Submit Offer</button>
        </form>
    {% else %}
        <p>请<a href="{% url 'login' %}?next={{ request.path }}">登录</a>后才能进行出价操作。</p>
    {% endif %}
{% endif %}

内容的提问来源于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 17:35:58