Reverse for ' ' with arguments ' ' not found. 1 pattern(s) tried: ['(?P<pk>[0-9]+)$']

I decide to make this post because i could not found answer to my problem on existing posts here and on Stack Overflow. But finally i found it so i want to share it with you.

I got this error:

Reverse for ‘article-view’ with arguments ‘(‘6’, ‘6’)’ not found. 1 pattern(s) tried: [’(?P[0-9]+)$’]

during processing this line of code:

 def get_absolute_url(self):
         return reverse('article-view', args=(str(self.id)))

The problem was with brackets with should be square.

args=[str(self.id)]

You can see that on django website.

https://docs.djangoproject.com/en/3.1/ref/urlresolvers/#django.urls.reverse

2 Likes

The problem is that you first intended to pass a tuple to args, but you actually didn’t.

When creating a tuple with a single element you must add a comma after the element to indicate it is indeed a tuple. Otherwise the parenthesis would have the usual purpose of enclosing information (x is (x)).

Therefore, in your case you should use: args=(str(self.id),). But of course your solution of passing a list instead is equally valid.