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

Django商品页评论功能点击后出现404错误排查求助

商品详情页评论功能404错误排查与修复

问题背景

在商品详情页实现评论添加功能时,点击评论按钮后触发404错误,提示“No List matches the given query”。该代码此前运行正常,现在出现异常。

相关代码

views.py

def item_detail(request, item_bid_id):
    get_item = get_object_or_404(List, pk=item_bid_id)
    get_item_2 = get_item.first_bid
    filter_item = Bidmodel.objects.filter(listb_id = item_bid_id)
    comment = Comments.objects.filter(listc_id = item_bid_id)
    
    context = {
        'item' : get_item,
        'now_bid': auxiliary(get_item_2, filter_item),
        'comments' : comment,
        }
    
    return render(request, 'auctions/item_detail.html', context )



# comment
@login_required(login_url="login")
def comments(request):
    comment = request.GET["comment"]
    username = request.user.username
    list_id = request.POST.get("listid", False)
    newcomment = Comments(user = username, comment = comment, listc_id = list_id)
    newcomment.save()
    return item_detail(request, list_id)

item_detail.html

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

{% block body %}
    {% if messages %}
        {% for message in messages %}
            <div>{{ message }}</div>
        {% endfor %}
    {% endif %}
    
    <img src= {{ item.image.url }} alt = "{{item.title}}"><br>
    <a><a>Product:</a>{{ item.title }}</a><br>
    <a><a>Category: </a>{{ item.category }}</a><br>
    <a><a>Desc: </a>{{ item.description }}</a><br>
    <a><a>Status: </a>{{ item.status }}</a><br>
    <a><a>Frist Bid: </a> {{ item.first_bid }} $ </a><br>
    <div><a><a>Current Bid: </a> {{ now_bid }} $ </div>
    <div><a><a>Listed By: </a> {{ item.user }}</div>
    


    <h3 id="h3">Comments</h3>
    <div>
        <ul>
            {% for comment in comments %}
                <li><a><a>{{ comment.user }} : {{comment.comment}}</a></li>
            {% endfor %}
        </ul>
    </div>

    <form method = "get" action ="{% url "comments" %}">
        <input required type = "text" placeholder = "Add Comment" name = "comment">
        <button type = "submit" value = {{ list.id }} name = "listid"> Comment </button>
    </form>
   
{% endblock%}

错误详情

  • 页面未找到(404):No List matches the given query
  • 请求方法:GET
  • 请求URL:http://127.0.0.1:8000/comments?comment=good
  • 触发源:auctions.views.comments
  • URLconf匹配顺序:
admin/
[name='index']
login [name='login']
logout [name='logout']
register [name='register']
auctions/<int:item_bid_id> [name='item_detail'] 
auxiliary [name='auxiliary']
comments [name='comments']

当前路径comments匹配最后一条规则。

修复步骤

1. 修正参数获取与请求方法不匹配问题

表单用GET方法提交,但视图里用request.POST.get("listid")获取参数,导致list_id始终为False,传递给item_detail后触发404。推荐改用POST方法提交评论(符合数据修改操作的HTTP规范):

  • 修改item_detail.html的表单:
<form method="post" action="{% url "comments" %}">
    {% csrf_token %}
    <input required type="text" placeholder="Add Comment" name="comment">
    <input type="hidden" name="listid" value="{{ item.id }}">
    <button type="submit"> Comment </button>
</form>
  • 修正views.py的comments函数:
@login_required(login_url="login")
def comments(request):
    if request.method == 'POST':
        comment_content = request.POST["comment"]
        list_id = request.POST.get("listid")
        
        # 验证商品ID有效性
        if not list_id or not List.objects.filter(pk=list_id).exists():
            messages.error(request, "无效的商品ID")
            return redirect('index')
        
        # 直接关联用户对象而非用户名(若Comments的user是外键)
        newcomment = Comments(user=request.user, comment=comment_content, listc_id=list_id)
        newcomment.save()
        
        # 重定向回商品详情页,而非直接调用视图函数
        return redirect('item_detail', item_bid_id=list_id)
    return redirect('index')

2. 修正模板变量错误

原模板中用{{ list.id }}传递商品ID,但上下文里的商品对象是item,需改为{{ item.id }},上述表单修改已包含此修正。

3. 规范视图返回逻辑

避免直接调用item_detail视图函数,改用redirect重定向到对应URL,防止重复提交问题,符合HTTP最佳实践。

4. 验证Comments模型外键配置

确保Comments模型的listc字段是指向List模型的外键:

class Comments(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    comment = models.TextField()
    listc = models.ForeignKey(List, on_delete=models.CASCADE)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 03:35:31