I'm getting the following error:
NoReverseMatch at /updatebooking/
Reverse for 'common.views.myview'
with arguments '()' and keyword arguments '{'msg': "hello", 'case':
'success'}' not found.
common/views.py
def view1(request):
...
return HttpResponseRedirect(reverse('common.views.view2', kwargs= {"msg":"hello","case":"success"}))
def view2(request,msg=None,case=None):
...
urls.py
url(r'^test1/$','common.views.view1',name='my_view1'),
url(r'^test2/$','common.views.view2',name='my_view2'),
This line reverse('common.views.view2', kwargs= {"msg":"hello","case":"success"}) is throwing the error.
The error comes only when I use kwargs. Following codes work:
return HttpResponseRedirect(reverse('my_view2'))
return HttpResponseRedirect(reverse('common.views.view2'))
Kindly help me resolve this.
When you are using reverse with kwargs parameters django tries to find a parameterized url route. In your example, matching route would be similar to
url(r'^test2/(?P<msg>\w+)/(?P<case>\w+)$','common.views.view2',name='my_view2')
Refer to reverse and URLDispatcher documentation for more details. Unfortunately, both URLDispatcher and reverse manuals are a little bit cryptic about this particuar feature.
Your url doesn't have msg or case parameter, that's why Django can't find it. See the paramenter slugin this example:
url(r'^(?P<slug>[^/]+)/$', 'test.views.detail', name="test-detail")
I believe that you're trying to pass a message/notification to another view. If that's the case, you should check the messages framework.
Related
I am following the polls app and I am copying everything making sure I have 100% the same code they have but when I use
response = client.get(reverse('polls:index'));
I get a huge error and none of the notes are making sense to me. I was told by someone it has to do with my views.py but I looked and the Django site and it is 100% the same as mine. This was the error I got:
django.urls.exceptions.NoReverseMatch: Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['pools/(?P<question_id>[0-9]+)/vote/$']
Looks like you have to specify the GET parameter named question_id on your URL.
The NoReverseMatch exception is raised by django.urls when a matching
URL in your URLconf cannot be identified based on the parameters
supplied.
The error said you have to provide a question id. I used 1 as an example.
http://localhost:8000/pools/1/vote/
I am trying to pass a parameter to my view, but I keep getting this error:
NoReverseMatch at /pay/how
Reverse for 'pay_summary' with arguments '(False,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['pay/summary/$']
/pay/how is the current view that I'm at. (that is the current template that that view is returning).
urls.py
url(r'^pay/summary/$', views.pay_summary, name='pay_summary')
views.py
def pay_summary(req, option):
if option:
#do something
else:
#do something else
....
template
my link
EDIT
I want the view should accept a POST request, not GET.
To add to the accepted answer, in Django 2.0 the url syntax has changed:
path('<int:key_id>/', views.myview, name='myname')
Or with regular expressions:
re_path(r'^(?P<key_id>[0-9])/$', views.myview, name='myname')
You need to define a variable on the url. For example:
url(r'^pay/summary/(?P<value>\d+)/$', views.pay_summary, name='pay_summary')),
In this case you would be able to call pay/summary/0
It could be a string true/false by replacing \d+ to \s+, but you would need to interpret the string, which is not the best.
You can then use:
my link
I use django-grappelli to create orderable inlines on the admin site. Occasionally (not reproducibly - about 50% of the time, which is particularly weird), Django throws the following exception when I try to save the ordering from the inline:
Exception Type: NoReverseMatch
Exception Value: Reverse for 'grp_related_lookup' with arguments '()' and keyword arguments '{}' not found.
Exception Location: /usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py in render, line 424
The offending line is this:
$("#id_" + this).grp_related_fk({lookup_url:"{% url 'grp_related_lookup' %}"});
As per the advice given in this related thread, I've tried quickly testing it in the shell, but it seems to work fine:
>>> from django.core.urlresolvers import reverse
>>> print reverse('grp_related_lookup')
/grappelli/lookup/related/
I'm at a loss. Has anyone made a similar experience?
Django version is 1.5.1.
You probably forgot to add grappelli urls into your urls.py (at least it was the case for me)
url(r'^grappelli/', include('grappelli.urls')),
error message:
ExceptionType: NoReverseMatch
Exception Value: Reverse for 'darts.teams.views.expanded_details' with arguments '(u'RightFlights',)' and keyword arguments '{}' not found.
in the template:
Expanded Details
in urls.py
urlpatterns = patterns('darts.teams.views',
url(r'^(?P<teamname>.*?)/expanded_details/$', 'team_details', {'expanded': True}, "expanded_details"),
url(r'^(?P<teamname>.*?)/details/$', 'team_details', name="team_details"),
url(r'^(?P<teamname>.*?)/add_player/$', 'team_add_player', name="team_add_player"),
url(r'^(?P<teamname>.*?)/add_player/confirm/$', 'team_add_player',"team_add_player_confirm"),
)
The additional URLs in urls.py all render fine, but the 'expanded_details' one is throwing the error.
Why is this one different than the others? Am I missing something blatant?
update
The error says "Reverse for 'darts.teams.views.expanded_details' failed", but it should be 'darts.teams.views.team_details' or 'expanded_details'. The first form is the path to view, the latter is the name of the named URL.
You could check the value of expanded_details inside the templatetag to ensure it is resolved to one of the correct values above, or follow slackjake's suggestion: use 'expanded_details' (note the single quote) directly.
(?P<teamname>.*? is invalid, maybe you mean (?P<teamname>.*?)?
Also, what does lib.url do?
I'm trying to pass multiple args in django's HttpResponseRedirect reverse function, but I keep getting this error
Reverse for 'DemoVar.views.success' with arguments '()' and keyword arguments '{'company': u'Company ', 'sid': 47606734}' not found.
#Calling function
return HttpResponseRedirect(reverse('DemoVar.views.success',kwargs={'sid':int(ph), 'company':company}))
#Function definition
def success(request, sid, company):
#urls.py
url(r'^success/(?P<sid>[\d]+)/(?P<company>[\w\d-]+)/$','success', name='DemoVar_success'),
I tried passing 'args' in reverse function, but with similar error. Please help.
Update: Tried passing the args as a tuple. The output remains same.
return HttpResponseRedirect(reverse('DemoVar.views.success',args=(ph, company,)))
#Error
Reverse for 'DemoVar.views.success' with arguments '(47606734, 'Dummy Company')' and keyword arguments '{}' not found.
My regex inside urls.py for corresponding function url was wrong. Apparently Django looks at urls.py to figure out the type of inputs for the view function, which in my case was 'DemoVar.views.success'. Since the url's regex didn't allow for 'spaces', django gave an error. I changed to regex to accommodate an input containing spaces.
url(r'^success/(?P<sid>[\d]+)/(?P<company>[\s\w\d-]+)/$','success', name='DemoVar_success'),
Your URL is named DemoVar_success not DemoVar.views.success.