简体   繁体   English

Django:在HttpResponseRedirect反向函数中传递多个参数

[英]Django : Passing multiple args in HttpResponseRedirect reverse function

I'm trying to pass multiple args in django's HttpResponseRedirect reverse function, but I keep getting this error 我正在尝试在Django的HttpResponseRedirect反向函数中传递多个args,但我一直收到此错误

Reverse for 'DemoVar.views.success' with arguments '()' and keyword arguments '{'company': u'Company ', 'sid': 47606734}' not found. 找不到带有参数'()'和关键字参数'{'company':u'Company','sid':47606734}'的'DemoVar.views.success'。

 #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. 我尝试在反向函数中传递“ args”,但出现类似错误。 Please help. 请帮忙。

Update: Tried passing the args as a tuple. 更新:尝试将args作为元组传递。 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. 我在urls.py中的正则表达式对应的函数url错误。 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'. 显然,Django会查看urls.py来确定视图函数的输入类型,在我的例子中是“ DemoVar.views.success”。 Since the url's regex didn't allow for 'spaces', django gave an error. 由于网址的正则表达式不允许使用“空格”,因此django提供了错误。 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'),

您的URL名为DemoVar_success而不是DemoVar.views.success

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM