Django : NoReverseMatch error when reverse a url - django

while redirecting to particular url i am trying to pass a variable (cat) to a view. using the reverse
like :
return HttpResponseRedirect(reverse('show_poll'), args=[cat])
above redirect will go to a following view :
def show_poll(request, cat):
print cat
having url as
url(r'^show/(?P<cat>\w+)/$', 'pollsite.views.show_poll', name="show_poll"),
getting : Reverse for 'show_poll' with arguments '()' and keyword arguments '{}' not found.
what am i missing here ?

Pass args to reverse() as
return HttpResponseRedirect(reverse('show_poll', args=[cat]))
#-----------------------------------------------^ closing bracket moved at the end

Related

urls django, NoReverseMatch

I have this error and I don't know it's origin :
NoReverseMatch at /PROJETTEST/rec
Reverse for 'myp' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['(?P\w+)$']
url(r'^allp$', allp.as_view(), name="allp"),
url(r'^allp/(?P<slug>[^/]+)$',ficheP.as_view(), name="ficheP"),
url(r'^(?P<namep>\w+)$','myp', name="myp"),
url(r'^(?P<namep>\w+)/members$','members', name="members"),
url(r'^(?P<namep>\w+)/rec','rec', name="rec"),
Gen
Members
rec
I have when I want access to ..//rec..
p.name is an empty string in your template. While myp url requires at least one alphanumeric character as the argument.

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?
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/

Reverse for 'note' with arguments '(1,)' and keyword arguments '{}' not found error in Django

My URL:
url(r'^(?P<task_id>\d+)/note/$', login_required(NoteView.as_view()), name='note'),
After note is successfully saved, I want to redirect to the note url with the task_id thus I am using reverse function:
return HttpResponseRedirect(reverse('website.views.note', args=(task_id,)))
My URL looks like this:
http://localhost:8000/1/note/
I think the reverse works only http://localhost:8000/note/1/ but not on http://localhost:8000/1/note/. How can I make it work?
You have given the URL a specific name - 'note'. That means you can't reverse it as 'website.views.note', but only as 'note'.
reverse('note', kwargs={'task_id':task_id})
Try passing the keyword args as
return HttpResponseRedirect(reverse('website.views.note', kwargs={'task_id':task_id,}))
Write :
return HttpResponseRedirect(reverse('note', kwargs={'task_id':task_id,}))
URL Name Should be unique. Here, "note" should be unique. Just pass "note" in HttpResponseRedirect.

Django URL Conf For Keyword Argument

What's wrong with the URL conf below:
url(
r'^outgoing-recommendations(?P<entry>\w+)/$',
login_required(outgoing_messages),
name='outgoing-recommendations',
),
Here is the invocation:
return redirect('outgoing-recommendations', kwargs={'entry':'outgoing'})
Here is the view function:
def outgoing_messages(request,entry):
user = User.objects.get(pk=request.session['user_id'])
I'm getting the error below:
Reverse for 'outgoing-recommendations' with arguments '()' and keyword arguments '{'kwargs': {'entry': 'outgoing'}}' not found.
The URL should look like this
url(r'^outgoing-recommendations/(?P<entry>\w+)/$',login_required(outgoing_messages), name='outgoing-recommendations'),
So you forgot your / on outgoing-recommendations.
Also you should call your redirect like this
return redirect('outgoing-recommendations', entry='outgoing')
and leave off the kwargs={} part, because what's happening is that you're trying to send in the keyworded arguments kwargs with it's nested kwargs.
But what I think you actually want is this
return redirect(reverse('outgoing-recommendations', kwargs={'entry':'outgoing'}))

Reverse for '' with arguments and keyword arguments '' not found

I have following views in my light_shop app:
def order_list(request, error_message):
context = {}
context['type'] = 'order-list'
context['error_message'] = error_message
update_context(request, context, add_order_list=True)
return render(request, 'light_shop/order_list.html', context)
def add_to_list(request, prd_id):
add_product_to_list(request, prd_id)
return HttpResponseRedirect(reverse('light_shop.views.order_list', args=('test_error',)))
and this is urls.py
urlpatterns = patterns('light_shop',
...
url(r'^add-to-list/(?P<prd_id>\d+)/$', 'views.add_to_list'),
url(r'^show-list/()$', 'views.order_list'),
...
)
but I get error: Reverse for 'light_shop.views.order_list' with arguments '('test_error',)' and keyword arguments '{}' not found. in add_to_list second line.
I even test naming parameter in url pattern for order_list. (e.g. url(r'^show-list/(?P<error_message>)$', 'views.order_list') and changing reverse function to reverse('light_shop.views.order_list', kwargs={'error_message':'error_message'})) but again same error is occurred.
I'm using Django 1.5 and I look this page for documantation and I am really confused what is the problem:
https://docs.djangoproject.com/en/1.5/topics/http/urls/
The problem is with the URL pattern
url(r'^show-list/()$', 'views.order_list'),
which seems to be incomplete.
Update it to (Basically, specify a Named Group)
url(r'^show-list/(?P<error_message>[\w_-]+)$', 'views.order_list'),