Horai Nuri
Horai Nuri

Reputation: 5568

Reverse for 'url_name' with arguments '()' and keyword arguments '{}' not found

I'm getting this error after redirecting on a form submit and I don't understand why it happens, I know that posts about this subject aren't missing but after reading dozens of them I'm still not able to fix this issue.

Reverse for 'commenting_room_detail' with arguments '()' and keyword arguments '{}' not found.  
1 pattern(s) tried: ['room/(?P<gig>\\d+)/(?P<name>[-\\w\\d]+)/$']

Here is what my code looks like :

views.py

if request.method == 'POST':
    form = MessageForm(request.POST)
    if form.is_valid():
        save_it = form.save(commit=False)
        ...
        save_it.save()
        return redirect(reverse('commenting_room_detail'))

urls.py

url(r'^room/(?P<gig>\d+)/(?P<name>[-\w\d]+)/$', views.commenting_room, name='commenting_room_detail'),

template/room.html

<form method="POST" action="{% url 'commenting_room_detail' room.gig.id request.user %}">...</form>

If someone could explain to me why this error is appearing on this specific code It would help me avoid this common error for the next times, because everything seems correct, thanks.


Update here I changed the url destination as shown on the answers below :

return redirect(reverse('commenting_room_detail'), kwargs={'gig': room.gig.id, 'name': request.user})

I still get this error, if the problem is the urls regex patterns how can I resolve this ?

Reverse for 'commenting_room_detail' with arguments '()' and keyword arguments '{}' not found. 
1 pattern(s) tried: ['room/(?P<gig>\\d+)/(?P<name>[-\\w\\d]+)/$']

Upvotes: 0

Views: 116

Answers (2)

Dmitry Shilyaev
Dmitry Shilyaev

Reputation: 733

Here is working example:

# views.py
class commenting_room(View):
    pass

# urls.py
url(r'^room/(?P<gig>\d+)/(?P<name>[-\w\d]+)/$', views.commenting_room, name='commenting_room_detail'),

# in code

# NoReverseMatch: Reverse for 'commenting_room_detail' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['room/(?P<gig>\\d+)/(?P<name>[-\\w\\d]+)/$']
reverse('commenting_room_detail')

# success
reverse('commenting_room_detail', kwargs={'gig': 123, 'name': 'test1'})

This is your error:

Reverse for 'url_name' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['room/(?P\d+)/(?P[-\w\d]+)/$']

Args issue error:

Reverse for 'commenting_room_detail' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['room/(?P\d+)/(?P[-\w\d]+)/$']

Double check your url patterns.

Upvotes: 1

Moses Koledoye
Moses Koledoye

Reputation: 78536

You need to pass the required keyword arguments for that url to reverse in your view function like you did in your template:

return redirect(reverse('commenting_room_detail', kwargs={...}))
#                                                 ^^^^^^

Upvotes: 1

Related Questions