0

I've got the following method in my View:

@action(detail=True, methods=['delete'])
def remove(self, request, *args,**kwargs):
    print("here")

I've created a test for this endpoint. But haven't been able to get the correct url.

When I do

url = reverse('order-remove', kwargs={'pk':1234, 'product_id':1})

I get the following error:

django.urls.exceptions.NoReverseMatch: Reverse for 'order-remove' with a keyword arguments ... not found. 2 pattern(s) tried: ...

and when I try:

pk=1234
product_id=1
url = reverse('order-remove', args=(pk, product_id,))

I got the following url:

/orders/1234/remove.1

instead of what I'd be expecting which is: orders/1234/remove/4 with an / instead of .

What am I missing?

1 Answers1

0

Try(check) this:

urls.py:

url(r'^orders/remove/(?P<pk>\d+)/(?P<product_id>\d+)/$', 'views.remove', name='order_remove'),

views.py:

    ....
    reverse('order_remove', kwargs={'pk':1,'product_id': 12})

Output:

orders/remove/1/12/

Pradip Kachhadiya
  • 2,067
  • 10
  • 28
  • but that's not the url pattern I'm looking for. The one you're suggesting is `orders/remove/1/12` which is a wrong patter. The one I'm looking for is `/orders/{order_id}/remove/{item_id}`. Thanks – Troy McClure Mar 06 '21 at 18:08
  • @TroyMcClure share your urls in this question – Pradip Kachhadiya Mar 06 '21 at 18:11
  • 1
    found the issue. I needed to add `url_path` to my action, so it ended up being: `@action(detail=True, methods=['post'], url_path='remove/(?P\d+)')`. Thanks – Troy McClure Mar 06 '21 at 21:02