没有列表与给定的查询匹配

问题描述 投票:0回答:1

我希望能够在产品页面添加评论,但是点击评论按钮后,出现此错误。问题是什么? 我之前尝试过这段代码,它运行得很好,但现在它给出了错误。

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%}``

并且与评论相关的表也已创建。

python html django django-models comments
1个回答
0
投票

您遇到的错误可能是由于 item_detail.html 模板中的 form 方法造成的。您已将方法设置为“get”而不是“post”。由于您尝试将评论保存在数据库中,因此它应该是 POST 请求。

要修复该错误,您需要将表单中的方法更改为“发布”,并将操作更新为指向评论视图。

替换 item_detail.html 模板中的这一行:

html

和:

html

此外,您应该在表单中包含 CSRF 令牌,以防止跨站点请求伪造攻击。在表单标签内添加“{% csrf_token %}”模板标签:
<form method="post" action="{% url "comments" %}">
    {% csrf_token %}
    <!-- Rest of the form content -->
</form>

确保更新views.py 文件中的评论视图以正确处理POST 请求。您可以使用

request.POST.get()
方法从 POST 请求中检索评论数据。

这是您的评论视图的更新版本:

@login_required(login_url="login")
def comments(request):
    if request.method == "POST":
        comment = request.POST.get("comment")
        username = request.user.username
        list_id = request.POST.get("listid")
        newcomment = Comments(user=username, comment=comment, listc_id=list_id)
        newcomment.save()
        return redirect("item_detail", item_bid_id=list_id)
    else:
        return HttpResponseNotAllowed(["POST"])

通过这些更改,您应该能够在产品页面上添加评论而不会遇到任何错误。

© www.soinside.com 2019 - 2024. All rights reserved.