Ollyver
Ollyver

Reputation: 367

NoReverseMatch exception - redirect with keyword arguments

I am getting a NoReverseMatch error when trying to use Django's redirect, and cannot figure out what I am doing wrong.

In views:

def addConjugatedForm(request):
    #update database
    return redirect('findConjugatedForms', chapter=ch.chapter, verse=verse.verse, book=b.name)

def findConjugatedForms(request, book, chapter, verse):
    #return a view

In urls.py:

url(r'^findConjugatedForms/(?P<book>\w+)/(?P<chapter>\d+)/(?P<verse>\d+)/', 'findConjugatedForms'),

The url works as a url, i.e. "...findConjugatedForms/Romans/3/24" returns a page. But when a form on that page is submitted, it won't redirect back.

I have named arguments in my url (which seems to be the common problem in other questions on Stack Overflow) and the values of the kwargs seem to be correct.

Reverse for 'findConjugatedForms' with arguments '()' and keyword arguments '{'chapter': 3, 'verse': 24, 'book': u'Romans'}' not found. 0 pattern(s) tried: []

What am I missing?

Upvotes: 0

Views: 367

Answers (1)

okm
okm

Reputation: 23871

redirect uses reverse internally. You need to define a viewname for the url, or use a Python path or pass a callable object:

url(r'^findConjugatedForms/(?P<book>\w+)/(?P<chapter>\d+)/(?P<verse>\d+)/',
    'findConjugatedForms',
    name='findConjugatedForms')  # Define a name

Besides, add a suffix '$' into the pattern to prevent the url from matching other urls that starts with findConjugatedForms/book/chapter/verse/

Upvotes: 1

Related Questions