django NoReverseMatch at /AddBid/1 Reverse for'addComment' with keyword arguments '' id ':未找到“}”,已尝试1种模式:['addComment/(?P < id>[0-9]+)\\Z ']

u4dcyp6a  于 2023-06-25  发布在  Go
关注(0)|答案(1)|浏览(84)

当我点击出价时,我收到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 %}

网址

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")
]

我试图改变在视图和url的html上传递的id参数的名称,但无法解决问题。

mqxuamgl

mqxuamgl1#

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

context = {
   'listing': listingBid
}
return render(request, "auctions/listing.html", context)

相关问题