NoReverseMatch at /AddBid/1 Reverse for'addComment' withkeyword arguments '{'id': ''}' not found. 1 pattern(s) tried: ['addComment/(?P<id>[0-9]+)\\Z']

huangapple go评论165阅读模式
英文:

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似乎没有传递。

视图:

  1. def listing(request, listing_id):
  2. listing = Listing.objects.get(pk=listing_id)
  3. IsWatchlist = request.user in listing.watchlist.all()
  4. allComments = Comment.objects.filter(listing=listing)
  5. return render(request, "auctions/listing.html", {
  6. "listing": listing,
  7. "ItemisinIsWatchlist": IsWatchlist,
  8. "allComments":allComments
  9. })
  10. @login_required
  11. def addwatchlist(request, id):
  12. listingData = Listing.objects.get(pk= id)
  13. current_user= request.user
  14. listingData.watchlist.add(current_user)
  15. return HttpResponseRedirect(reverse("listing", args=(id, )))
  16. @login_required
  17. def removewatchlist(request, id):
  18. listingData = Listing.objects.get(pk= id)
  19. current_user= request.user
  20. listingData.watchlist.remove(current_user)
  21. return HttpResponseRedirect(reverse("listing", args=(id, )))
  22. def addComment(request, id):
  23. currentUser = request.user
  24. listingData = Listing.objects.get(pk=id)
  25. message = request.POST["newComment"]
  26. newComment = Comment(
  27. author = currentUser,
  28. listing = listingData,
  29. message = message
  30. )
  31. newComment.save()
  32. return HttpResponseRedirect(reverse("listing", args=(id, )))
  33. @login_required
  34. def AddBid(request, id):
  35. listingBid = Listing.objects.get(pk=id)
  36. newBid = request.POST["newBid"]
  37. if int(newBid) > listingBid.bid.bid:
  38. updateBid= Bid(bid=int(newBid), user=request.user)
  39. updateBid.save()
  40. listingBid.bid.bid = int(newBid)
  41. listingBid.save()
  42. return render(request, "auctions/listing.html")
  43. else:
  44. return render(request, "auctions/listing.html")

HTML:

  1. {% if user.is_authenticated %}
  2. <p>
  3. {% if ItemisinIsWatchlist %}
  4. <form action="{% url 'removewatchlist' id=listing.id %}" method="post">
  5. {% csrf_token %}
  6. <button type="submit" class="btn btn-danger">Remove from watchlist</button>
  7. </form>
  8. {% else %}
  9. {% if listing.id %}
  10. <form action="{% url 'addwatchlist' id=listing.id %}" method="post">
  11. {% csrf_token %}
  12. <button type="submit" class="btn btn-success">Add to watchlist</button>
  13. </form>
  14. {% else %}
  15. <p>No listing ID available.</p>
  16. {% endif %}
  17. {% endif %}
  18. </p>
  19. <p>
  20. {% if listing.id %}
  21. <h2>Bids</h2>
  22. <form action="{% url 'AddBid' id=listing.id %}" method="POST">
  23. {% csrf_token %}
  24. <input type="number" min="0" name="newBid" placeholder="Add new Bid">
  25. <button type="submit" class="btn btn-primary">Submit</button>
  26. </form>
  27. {% else %}
  28. <p>No listing ID available.</p>
  29. </p>
  30. {% endif %}
  31. {% endif %}
  32. <p class="card-text"><small class="text-muted">Publisher: {{listing.publisher}}</small></p>
  33. </div>
  34. </div>
  35. <p class="row mx-3">
  36. <h2>Comments</h2>
  37. <br>
  38. {% if user.is_authenticated %}
  39. <form action="{% url 'addComment' id=listing.id %}" method="POST">
  40. {% csrf_token %}
  41. <input type="textarea" name="newComment" placeholder="Add new comment">
  42. <button type="submit" class="btn btn-primary">Submit</button>
  43. </form>
  44. </p>
  45. {% endif %}
  46. <br/>
  47. <ul class="list-group">
  48. {% for comment in allComments %}
  49. <li class="list-group-item">{{comment.message}}</li>
  50. <li class="list-group-item"> Posted by: {{comment.author}}</li>
  51. {% endfor %}
  52. </ul>
  53. {% endblock %}

URLS:

  1. urlpatterns = [
  2. path("", views.index, name="index"),
  3. path("login", views.login_view, name="login"),
  4. path("logout", views.logout_view, name="logout"),
  5. path("register", views.register, name="register"),
  6. path('create', views.create, name="create"),
  7. path("<int:listing_id>", views.listing, name="listing"),
  8. path("removewatchlist/<int:id>", views.removewatchlist, name="removewatchlist"),
  9. path("addwatchlist/<int:id>", views.addwatchlist, name="addwatchlist"),
  10. path("watchlist", views.watchlist, name="watchlist"),
  11. path("addComment/<int:id>", views.addComment, name="addComment"),
  12. path("AddBid/<int:id>", views.AddBid, name="AddBid")
  13. ]
英文:

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:

  1. def listing(request, listing_id):
  2. listing = Listing.objects.get(pk=listing_id)
  3. IsWatchlist = request.user in listing.watchlist.all()
  4. allComments = Comment.objects.filter(listing=listing)
  5. return render(request, &quot;auctions/listing.html&quot;, {
  6. &quot;listing&quot;: listing,
  7. &quot;ItemisinIsWatchlist&quot;: IsWatchlist,
  8. &quot;allComments&quot;:allComments
  9. })
  10. @login_required
  11. def addwatchlist(request, id):
  12. listingData = Listing.objects.get(pk= id)
  13. current_user= request.user
  14. listingData.watchlist.add(current_user)
  15. return HttpResponseRedirect(reverse(&quot;listing&quot;, args=(id, )))
  16. @login_required
  17. def removewatchlist(request, id):
  18. listingData = Listing.objects.get(pk= id)
  19. current_user= request.user
  20. listingData.watchlist.remove(current_user)
  21. return HttpResponseRedirect(reverse(&quot;listing&quot;, args=(id, )))
  22. def addComment(request, id):
  23. currentUser = request.user
  24. listingData = Listing.objects.get(pk=id)
  25. message = request.POST[&quot;newComment&quot;]
  26. newComment = Comment(
  27. author = currentUser,
  28. listing = listingData,
  29. message = message
  30. )
  31. newComment.save()
  32. return HttpResponseRedirect(reverse(&quot;listing&quot;, args=(id, )))
  33. @login_required
  34. def AddBid(request, id):
  35. listingBid = Listing.objects.get(pk=id)
  36. newBid = request.POST[&quot;newBid&quot;]
  37. if int(newBid) &gt; listingBid.bid.bid:
  38. updateBid= Bid(bid=int(newBid), user=request.user)
  39. updateBid.save()
  40. listingBid.bid.bid = int(newBid)
  41. listingBid.save()
  42. return render(request, &quot;auctions/listing.html&quot;)
  43. else:
  44. return render(request, &quot;auctions/listing.html&quot;)

HTML:

  1. {% if user.is_authenticated %}
  2. &lt;p&gt;
  3. {% if ItemisinIsWatchlist %}
  4. &lt;form action=&quot;{% url &#39;removewatchlist&#39; id=listing.id %}&quot; method=&quot;post&quot;&gt;
  5. {% csrf_token %}
  6. &lt;button type=&quot;submit&quot; class=&quot;btn btn-danger&quot;&gt;Remove from watchlist&lt;/button&gt;
  7. &lt;/form&gt;
  8. {% else %}
  9. {% if listing.id %}
  10. &lt;form action=&quot;{% url &#39;addwatchlist&#39; id=listing.id %}&quot; method=&quot;post&quot;&gt;
  11. {% csrf_token %}
  12. &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;Add to watchlist&lt;/button&gt;
  13. &lt;/form&gt;
  14. {% else %}
  15. &lt;p&gt;No listing ID available.&lt;/p&gt;
  16. {% endif %}
  17. {% endif %}
  18. &lt;/p&gt;
  19. &lt;p&gt;
  20. {% if listing.id %}
  21. &lt;h2&gt;Bids&lt;/h2&gt;
  22. &lt;form action=&quot;{% url &#39;AddBid&#39; id=listing.id %}&quot; method=&quot;POST&quot;&gt;
  23. {% csrf_token %}
  24. &lt;input type=&quot;number&quot; min=&quot;0&quot; name=&quot;newBid&quot; placeholder=&quot;Add new Bid&quot;&gt;
  25. &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;
  26. &lt;/form&gt;
  27. {% else %}
  28. &lt;p&gt;No listing ID available.&lt;/p&gt;
  29. &lt;/p&gt;
  30. {% endif %}
  31. {% endif %}
  32. &lt;p class=&quot;card-text&quot;&gt;&lt;small class=&quot;text-muted&quot;&gt;Publisher: {{listing.publisher}}&lt;/small&gt;&lt;/p&gt;
  33. &lt;/div&gt;
  34. &lt;/div&gt;
  35. &lt;p class=&quot;row mx-3&quot;&gt;
  36. &lt;h2&gt;Comments&lt;/h2&gt;
  37. &lt;br&gt;
  38. {% if user.is_authenticated %}
  39. &lt;form action=&quot;{% url &#39;addComment&#39; id=listing.id %}&quot; method=&quot;POST&quot;&gt;
  40. {% csrf_token %}
  41. &lt;input type=&quot;textarea&quot; name=&quot;newComment&quot; placeholder=&quot;Add new comment&quot;&gt;
  42. &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;
  43. &lt;/form&gt;
  44. &lt;/p&gt;
  45. {% endif %}
  46. &lt;br/&gt;
  47. &lt;ul class=&quot;list-group&quot;&gt;
  48. {% for comment in allComments %}
  49. &lt;li class=&quot;list-group-item&quot; &gt;{{comment.message}}&lt;/li&gt;
  50. &lt;li class=&quot;list-group-item&quot; &gt; Posted by: {{comment.author}}&lt;/li&gt;
  51. {% endfor %}
  52. &lt;/ul&gt;
  53. {% endblock %}

URLS

  1. urlpatterns = [
  2. path(&quot;&quot;, views.index, name=&quot;index&quot;),
  3. path(&quot;login&quot;, views.login_view, name=&quot;login&quot;),
  4. path(&quot;logout&quot;, views.logout_view, name=&quot;logout&quot;),
  5. path(&quot;register&quot;, views.register, name=&quot;register&quot;),
  6. path(&#39;create&#39;, views.create, name=&quot;create&quot;),
  7. path(&quot;&lt;int:listing_id&gt;&quot;, views.listing, name=&quot;listing&quot;),
  8. path(&quot;removewatchlist/&lt;int:id&gt;&quot;, views.removewatchlist, name=&quot;removewatchlist&quot;),
  9. path(&quot;addwatchlist/&lt;int:id&gt;&quot;, views.addwatchlist, name=&quot;addwatchlist&quot;),
  10. path(&quot;watchlist&quot;, views.watchlist, name=&quot;watchlist&quot;),
  11. path(&quot;addComment/&lt;int:id&gt;&quot;, views.addComment, name=&quot;addComment&quot;),
  12. path(&quot;AddBid/&lt;int:id&gt;&quot;, views.AddBid, name=&quot;AddBid&quot;)
  13. ]

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

你需要将列表添加到渲染上下文中,以便将其从视图传递到模板渲染器,如下所示:

  1. context = {
  2. 'listing': listingBid
  3. }
  4. 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:

  1. context = {
  2. &#39;listing&#39;: listingBid
  3. }
  4. return render(request, &quot;auctions/listing.html&quot;, context)

huangapple
  • 本文由 发表于 2023年6月11日 22:53:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定