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

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

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, &quot;auctions/listing.html&quot;, {
&quot;listing&quot;: listing,
&quot;ItemisinIsWatchlist&quot;: IsWatchlist,
&quot;allComments&quot;: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(&quot;listing&quot;, 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(&quot;listing&quot;, args=(id, )))
def addComment(request, id):
currentUser = request.user
listingData = Listing.objects.get(pk=id)
message = request.POST[&quot;newComment&quot;]
newComment = Comment(
author = currentUser,
listing = listingData,
message = message
)
newComment.save()
return HttpResponseRedirect(reverse(&quot;listing&quot;, args=(id, )))
@login_required
def AddBid(request, id):
listingBid = Listing.objects.get(pk=id)
newBid = request.POST[&quot;newBid&quot;]
if int(newBid) &gt; listingBid.bid.bid:
updateBid= Bid(bid=int(newBid), user=request.user)
updateBid.save()
listingBid.bid.bid = int(newBid)
listingBid.save()
return render(request, &quot;auctions/listing.html&quot;)
else:
return render(request, &quot;auctions/listing.html&quot;)

HTML:

{% if user.is_authenticated %}     
&lt;p&gt;
{% if ItemisinIsWatchlist %}
&lt;form action=&quot;{% url &#39;removewatchlist&#39; id=listing.id %}&quot; method=&quot;post&quot;&gt;
{% csrf_token %}
&lt;button type=&quot;submit&quot; class=&quot;btn btn-danger&quot;&gt;Remove from watchlist&lt;/button&gt;
&lt;/form&gt;
{% else %}
{% if listing.id %}
&lt;form action=&quot;{% url &#39;addwatchlist&#39; id=listing.id %}&quot; method=&quot;post&quot;&gt;
{% csrf_token %}
&lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;Add to watchlist&lt;/button&gt;
&lt;/form&gt;
{% else %}
&lt;p&gt;No listing ID available.&lt;/p&gt;
{% endif %}
{% endif %}
&lt;/p&gt;
&lt;p&gt;
{% if listing.id %} 
&lt;h2&gt;Bids&lt;/h2&gt;
&lt;form action=&quot;{% url &#39;AddBid&#39; id=listing.id %}&quot; method=&quot;POST&quot;&gt;
{% csrf_token %} 
&lt;input type=&quot;number&quot; min=&quot;0&quot; name=&quot;newBid&quot; placeholder=&quot;Add new Bid&quot;&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;
&lt;/form&gt;
{% else %}
&lt;p&gt;No listing ID available.&lt;/p&gt;
&lt;/p&gt;
{% endif %}
{% endif %}
&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;
&lt;/div&gt;
&lt;/div&gt;
&lt;p class=&quot;row mx-3&quot;&gt;
&lt;h2&gt;Comments&lt;/h2&gt;
&lt;br&gt;
{% if user.is_authenticated %}
&lt;form action=&quot;{% url &#39;addComment&#39; id=listing.id %}&quot; method=&quot;POST&quot;&gt;
{% csrf_token %} 
&lt;input type=&quot;textarea&quot; name=&quot;newComment&quot; placeholder=&quot;Add new comment&quot;&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;
&lt;/form&gt;
&lt;/p&gt;
{% endif %}
&lt;br/&gt;
&lt;ul class=&quot;list-group&quot;&gt;
{% for comment in allComments %}
&lt;li class=&quot;list-group-item&quot; &gt;{{comment.message}}&lt;/li&gt;
&lt;li class=&quot;list-group-item&quot; &gt; Posted by: {{comment.author}}&lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
{% endblock %}

URLS

urlpatterns = [
path(&quot;&quot;, views.index, name=&quot;index&quot;),
path(&quot;login&quot;, views.login_view, name=&quot;login&quot;),
path(&quot;logout&quot;, views.logout_view, name=&quot;logout&quot;),
path(&quot;register&quot;, views.register, name=&quot;register&quot;),
path(&#39;create&#39;, views.create, name=&quot;create&quot;),
path(&quot;&lt;int:listing_id&gt;&quot;, views.listing, name=&quot;listing&quot;),
path(&quot;removewatchlist/&lt;int:id&gt;&quot;, views.removewatchlist, name=&quot;removewatchlist&quot;),
path(&quot;addwatchlist/&lt;int:id&gt;&quot;, views.addwatchlist, name=&quot;addwatchlist&quot;),
path(&quot;watchlist&quot;, views.watchlist, name=&quot;watchlist&quot;),
path(&quot;addComment/&lt;int:id&gt;&quot;, views.addComment, name=&quot;addComment&quot;),
path(&quot;AddBid/&lt;int:id&gt;&quot;, views.AddBid, name=&quot;AddBid&quot;)
]

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 = {
&#39;listing&#39;: listingBid
}
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:

确定