python - 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 }} <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 %}
解决方案
渲染 html 时,您尚未在上下文中发送项目。在上下文中添加项目并将工作。
return render(request,"auctions/item.html",{
...
'item': item
})
推荐阅读
- angular - Angular 严格模式、Ngrx、Jamsmine:带道具的模拟选择器
- c++ - MinGW不在日食上
- dictionary - 将值设置为字典数组中的特定元素会覆盖数组中的所有值
- salesforce - JSforce - 如何使用 OAuth2 而不将用户重定向到 Salesforce 页面以获得授权?
- sql-server - TSQL 比较数据并返回不同行的结果
- c++ - 两个不同的键可以在Redis C++中存储相同的数据而不重复吗
- android - 如何在格式化字符串资源中使用整数?
- python - OSError:SavedModel 文件不存在于:sh./model/traffic_sign.model/{saved_model.pbtxt|saved_model.pb}
- html - 关键帧动画在其他方向移动时没有过渡(纯 CSS)
- vue.js - 清除过滤器时,剑道时间选择器过滤器仅显示上午 12:00