首页 > 解决方案 > NOReverseMatch 在 /bid/2

问题描述

我在 /bid/2 错误处得到 NoReverseMatch 你能告诉我我做错了什么以及可能的解决方案。我是 Django 的新手,谢谢。忽略:- sadfgnasgdsfhfgjhn fgdshsdgh fd dgsdfg dsf ds asgdfhrtyn sdfgh ehe ereg sdfgsfghhsdje sdfghshsdherh 这是我的 URL 模式:-

urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path("create/", views.create_listing,name="create_listing"),
path("submit/<str:username>",views.submit,name="submit"),
path("<int:list_id>",views.item,name="item"),
path("comments/<int:list_id>",views.comment,name="comment"),
path("category/<int:item_category>",views.category,name="category"),
path("bid/<int:list_id>",views.set_bid,name="set_bid"),

]

这是我的视图功能:-

def set_bid(request,list_id):
price = request.POST['bid']
username = request.user.username
item = Listing.objects.get(pk=list_id)
b=Bids.objects.filter(item=item)
if b:
    b.update(bid=price)
    b.save()
else:
    b = Bids(bid=price,user=username,item=item)
    b.save()
item.current_bidder = username
item.save()
return render(request,"auctions/item.html",{
    "listings":Listing.objects.get(pk=list_id),
    "comments":Comments.objects.all().filter(comment_id=list_id),
    "bid":Bids.objects.all().filter(item=item),
})

这是调用 url 模式的项目模板:-

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

{% block body %}
{% if message %}
<div class="alert alert-danger ">{{ message }}</div>
{% endif %}
<div class="item-block">
<h3>Listing:  {{ item.name }}&nbsp;&nbsp;&nbsp;&nbsp;<button class="btn btn-primary" type="button">Add to Watch list</button></h3>
<img src="{{ item.image }}" alt="No Image">
<p>{{ item.description }}</p>
{% if bid %}
<h4>${{ bid.bid }} by {{ item.current_bidder }}</h4>
{% else %}
<h4>${{ item.price }}</h4>
{% endif %}
<form class="" action="{% url 'set_bid' item.id %}" method="post">
  {% csrf_token %}
  <input type="number" name="bid" placeholder="Bid"><br>
  <input id="bid" type="submit" value="Place Bid">
</form>
</div>
<h5>Details:-</h5>
<ul>
  <li>Listed by: {{ item.user }}</li>
  <li>Category: {{ item.category }}</li>
</ul>
<hr>
<h1>Comments</h1>
<!-- TODO:  -->
{% for comment in comments %}
<div class="comment">
  <h6>{{ comment.user }}</h6>
  <p>{{ comment.comment }}
    <button class="float-right"><a href=""><img src="https://img.icons8.com/office/16/000000/delete-sign.png"/></a></button>
  </p>
</div>
{% endfor %}
<form action="{% url 'comment' item.id %}" method="post">
  {% csrf_token %}
  <label for="Your comment">Your comment</label><br>
  <textarea name="comment" rows="5" cols="80"></textarea>
  <input type="submit" value="Post">
</form>
{% endblock %}

我得到的错误:- 在此处输入图像描述

标签: pythondjango

解决方案


渲染 html 时,您尚未在上下文中发送项目。在上下文中添加项目并将工作。

return render(request,"auctions/item.html",{
    ...
    'item': item
})

推荐阅读