英文:
NoReverseMatch at /AddBid/1 Reverse for'addComment' withkeyword arguments '{'id': ''}' not found. 1 pattern(s) tried: ['addComment/(?P<id>[0-9]+)\\Z']
问题
当我点击出价时,我收到了NoReverseMatch错误。
在我的视图中,我传递了不同的时间,并成功将id作为输入传递给视图。
但是在AddComment函数中,id似乎没有传递。
视图:
def listing(request, listing_id):
listing = Listing.objects.get(pk=listing_id)
IsWatchlist = request.user in listing.watchlist.all()
allComments = Comment.objects.filter(listing=listing)
return render(request, "auctions/listing.html", {
"listing": listing,
"ItemisinIsWatchlist": IsWatchlist,
"allComments":allComments
})
@login_required
def addwatchlist(request, id):
listingData = Listing.objects.get(pk= id)
current_user= request.user
listingData.watchlist.add(current_user)
return HttpResponseRedirect(reverse("listing", args=(id, )))
@login_required
def removewatchlist(request, id):
listingData = Listing.objects.get(pk= id)
current_user= request.user
listingData.watchlist.remove(current_user)
return HttpResponseRedirect(reverse("listing", args=(id, )))
def addComment(request, id):
currentUser = request.user
listingData = Listing.objects.get(pk=id)
message = request.POST["newComment"]
newComment = Comment(
author = currentUser,
listing = listingData,
message = message
)
newComment.save()
return HttpResponseRedirect(reverse("listing", args=(id, )))
@login_required
def AddBid(request, id):
listingBid = Listing.objects.get(pk=id)
newBid = request.POST["newBid"]
if int(newBid) > listingBid.bid.bid:
updateBid= Bid(bid=int(newBid), user=request.user)
updateBid.save()
listingBid.bid.bid = int(newBid)
listingBid.save()
return render(request, "auctions/listing.html")
else:
return render(request, "auctions/listing.html")
HTML:
{% if user.is_authenticated %}
<p>
{% if ItemisinIsWatchlist %}
<form action="{% url 'removewatchlist' id=listing.id %}" method="post">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Remove from watchlist</button>
</form>
{% else %}
{% if listing.id %}
<form action="{% url 'addwatchlist' id=listing.id %}" method="post">
{% csrf_token %}
<button type="submit" class="btn btn-success">Add to watchlist</button>
</form>
{% else %}
<p>No listing ID available.</p>
{% endif %}
{% endif %}
</p>
<p>
{% if listing.id %}
<h2>Bids</h2>
<form action="{% url 'AddBid' id=listing.id %}" method="POST">
{% csrf_token %}
<input type="number" min="0" name="newBid" placeholder="Add new Bid">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% else %}
<p>No listing ID available.</p>
</p>
{% endif %}
{% endif %}
<p class="card-text"><small class="text-muted">Publisher: {{listing.publisher}}</small></p>
</div>
</div>
<p class="row mx-3">
<h2>Comments</h2>
<br>
{% if user.is_authenticated %}
<form action="{% url 'addComment' id=listing.id %}" method="POST">
{% csrf_token %}
<input type="textarea" name="newComment" placeholder="Add new comment">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</p>
{% endif %}
<br/>
<ul class="list-group">
{% for comment in allComments %}
<li class="list-group-item">{{comment.message}}</li>
<li class="list-group-item"> Posted by: {{comment.author}}</li>
{% endfor %}
</ul>
{% endblock %}
URLS:
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, name="create"),
path("<int:listing_id>", views.listing, name="listing"),
path("removewatchlist/<int:id>", views.removewatchlist, name="removewatchlist"),
path("addwatchlist/<int:id>", views.addwatchlist, name="addwatchlist"),
path("watchlist", views.watchlist, name="watchlist"),
path("addComment/<int:id>", views.addComment, name="addComment"),
path("AddBid/<int:id>", views.AddBid, name="AddBid")
]
英文:
When I click on bid I receive the NoReverseMatch error.
In my views, I pass different times and successfully the id as input to the view.
When it comes to the AddComment function, the id seems not passed.
Views:
def listing(request, listing_id):
listing = Listing.objects.get(pk=listing_id)
IsWatchlist = request.user in listing.watchlist.all()
allComments = Comment.objects.filter(listing=listing)
return render(request, "auctions/listing.html", {
"listing": listing,
"ItemisinIsWatchlist": IsWatchlist,
"allComments":allComments
})
@login_required
def addwatchlist(request, id):
listingData = Listing.objects.get(pk= id)
current_user= request.user
listingData.watchlist.add(current_user)
return HttpResponseRedirect(reverse("listing", args=(id, )))
@login_required
def removewatchlist(request, id):
listingData = Listing.objects.get(pk= id)
current_user= request.user
listingData.watchlist.remove(current_user)
return HttpResponseRedirect(reverse("listing", args=(id, )))
def addComment(request, id):
currentUser = request.user
listingData = Listing.objects.get(pk=id)
message = request.POST["newComment"]
newComment = Comment(
author = currentUser,
listing = listingData,
message = message
)
newComment.save()
return HttpResponseRedirect(reverse("listing", args=(id, )))
@login_required
def AddBid(request, id):
listingBid = Listing.objects.get(pk=id)
newBid = request.POST["newBid"]
if int(newBid) > listingBid.bid.bid:
updateBid= Bid(bid=int(newBid), user=request.user)
updateBid.save()
listingBid.bid.bid = int(newBid)
listingBid.save()
return render(request, "auctions/listing.html")
else:
return render(request, "auctions/listing.html")
HTML:
{% if user.is_authenticated %}
<p>
{% if ItemisinIsWatchlist %}
<form action="{% url 'removewatchlist' id=listing.id %}" method="post">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Remove from watchlist</button>
</form>
{% else %}
{% if listing.id %}
<form action="{% url 'addwatchlist' id=listing.id %}" method="post">
{% csrf_token %}
<button type="submit" class="btn btn-success">Add to watchlist</button>
</form>
{% else %}
<p>No listing ID available.</p>
{% endif %}
{% endif %}
</p>
<p>
{% if listing.id %}
<h2>Bids</h2>
<form action="{% url 'AddBid' id=listing.id %}" method="POST">
{% csrf_token %}
<input type="number" min="0" name="newBid" placeholder="Add new Bid">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% else %}
<p>No listing ID available.</p>
</p>
{% endif %}
{% endif %}
<p class="card-text"><small class="text-muted">Publisher: {{listing.publisher}}</small></p>
</div>
</div>
<p class="row mx-3">
<h2>Comments</h2>
<br>
{% if user.is_authenticated %}
<form action="{% url 'addComment' id=listing.id %}" method="POST">
{% csrf_token %}
<input type="textarea" name="newComment" placeholder="Add new comment">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</p>
{% endif %}
<br/>
<ul class="list-group">
{% for comment in allComments %}
<li class="list-group-item" >{{comment.message}}</li>
<li class="list-group-item" > Posted by: {{comment.author}}</li>
{% endfor %}
</ul>
{% endblock %}
URLS
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, name="create"),
path("<int:listing_id>", views.listing, name="listing"),
path("removewatchlist/<int:id>", views.removewatchlist, name="removewatchlist"),
path("addwatchlist/<int:id>", views.addwatchlist, name="addwatchlist"),
path("watchlist", views.watchlist, name="watchlist"),
path("addComment/<int:id>", views.addComment, name="addComment"),
path("AddBid/<int:id>", views.AddBid, name="AddBid")
]
I tried to change name on the id parameter passed on the html on the views and urls but can't figure out the problem.
答案1
得分: 0
你需要将列表添加到渲染上下文中,以便将其从视图传递到模板渲染器,如下所示:
context = {
'listing': listingBid
}
return render(request, "auctions/listing.html", context)
英文:
you need to add listing to the render context to transfer it from the view to the template renderer like:
context = {
'listing': listingBid
}
return render(request, "auctions/listing.html", context)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论