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

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'),

Related

How to use redirect with multiple parameters in Django views?

I am trying to redirect to a function from another function in views.
But I am getting following error
NoReverseMatch at /sigma/status1/
Reverse for 'testview' with keyword arguments '{'amount': 1.000, 'stat':'Approved', 'ref': '10917'}' not found. 1 pattern(s) tried: ['sigma\\/status2/(?P<amount>\\d+)/(?P<stat>[a-z][A-Z]+)/(?P<ref>\\d+)/$']
Below is corresponding part of my views.py
return redirect(reverse('testview',kwargs={'amount':1.000,'stat':'Approved','ref':str(res['ref'])}))
def payment_status2(request,amount,stat,ref):
return render(request, 'confirm1.html')
Below is corresponding part of my urls.py
urlpatterns = [
url('status1/', views.payment_status1),
url(r'^status2/(?P<amount>\d+)/(?P<stat>[a-z][A-Z]+)/(?P<ref>\d+)/$', views.payment_status2,name="testview"),
]
Your stat regex is incorrect:
?P<stat>[a-z][A-Z]+
This indicates that the argument should start with a lowercase letter and then followed by one or more uppercase letters (such as aPPROVED). You should change it to:
?P<stat>[a-zA-Z]+
or
?P<stat>\w+

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/

URL with args does not reverse

I have the following url:
urlpatterns += patterns('app_common.views_settings',
url(r'([\w-]+)/(\d+)/settings/$', 'settings', name="configuration_homepage"),
url(r'(?P<short_name>[\w-]+)/(?P<product_id>\d+)/settings/modify/(?P<sim_id>\d+)/$', 'modify_sim', name="modify_sim"),
)
urlpatterns += patterns('app_common.views_operator',
url(r'^operator/$', 'choose_operator', name="choose_operator"),
url(r'^(?P<short_name>[\w-]+)/project/$', 'choose_project', name="choose_project"),
url(r'([\w-]+)/(\d+)/$', 'set_product', name="set_product"),
url(r'^(?P<short_name>[\w-]+)/$', 'set_operator', name="set_operator"),
)
I tried to reverse configuration homepage using:
url = reverse('configuration_homepage', kwargs={short_name, product_id})
return HttpResponseRedirect(url)
Some times it works, but other times if failed with this issue (short_name=OCI and product_id=1)
Exception Type: NoReverseMatch
Exception Value: Reverse for 'configuration_homepage' with arguments '(u'1', u'OCI')' and keyword arguments '{}' not found.
If you guys detect something wrong in my code fell free to tell me... I tried to give name to variable but urls are not found in that case.
Use args instead of kwargs,
url = reverse('configuration_homepage', args=[short_name, product_id])
Your kwargs is wrong, you are passing a set() instead of a dict()
What you (probably) want is:
url = reverse('configuration_homepage',
kwargs={short_name: short_name, product_id: product_id})
This is one of the many reasons why I prefer dict(a=1, b=2) over {a:1, b:2} when possible,

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'}))

Django : NoReverseMatch error when reverse a url

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