Neohary
Neohary

Reputation: 59

django returns NoReverseMatch but it actually got the value

I'm making a shopping cart app for my website, and the function that worked yesterday was broken today.

I got Reverse for 'delete-cart' with arguments '(4,)' not found. 1 pattern(s) tried: ['cart/delete_cart/<int:pk>']error, But you can see that it has displayed the value of the parameter, proving that it got the value but still returned the not found error, why? Here is my relevant code:

urls.py

urlpatterns = [
    path('',views.myCartListView,name='my-cart'),
    url(r'^delete_cart/<int:pk>',views.delete_cart,name='delete-cart'),
]

views.py

@login_required
def myCartListView(request):
    context = {}
    if request.user:
        data = Cart.objects.filter(client=request.user)
    context['cart'] = data
    return render(request,'cart/my_cart.html',context)

@login_required
def delete_cart(request,pk):
    record = Cart.objects.get(pk=pk)
    record.delete()
    return redirect('cart/')

my_cart.html

{% for c in cart %}
    <tr>
        <th>{{c.item}}</th>
        <th>{{c.quantity}}</th>
        <th>{{c.price}}</th>
        <th>{{c.get_total_price}}</th>
        <th><a href="{% url 'delete-cart' c.id %}">DELETE</a></th>
    </tr>
{% endfor %}

Upvotes: 2

Views: 29

Answers (2)

ytsejam
ytsejam

Reputation: 3439

change your url pattern to

path('delete_cart/<int:pk>/',views.delete_cart,name='delete-cart')

Upvotes: 2

Sergey Pugach
Sergey Pugach

Reputation: 5669

Try to specify it as key value argument:

    <th><a href="{% url 'delete-cart' pk=c.id %}">DELETE</a></th>

Upvotes: 2

Related Questions