使用参数重定向.NoReverseMatch在/test/ [英] Redirect with parameters. NoReverseMatch at /test/

查看:39
本文介绍了使用参数重定向.NoReverseMatch在/test/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

views.py:

def demo(request, **kwargs):
    print response
    ......

def test(request):
    ......
    kwargs = {'response': response}
    return redirect('demo', **kwargs)

urls.py:

from django.conf.urls import patterns, url
from django.contrib.sitemaps.views import sitemap


urlpatterns = patterns('clients.views',
    url(r'^test/', 'test', name='test'),
    url(r'^demo/', 'demo', name='demo'),

)

为什么出现此错误:

/test/

带有参数'()'和关键字参数的'demo'的反向找不到'{'response':{u'status':u'ok'}}'.

Reverse for 'demo' with arguments '()' and keyword arguments '{'response': {u'status': u'ok'}}' not found.

请求方法:POST请求URL:http://127.0.0.1:8000/test/

Request Method: POST Request URL: http ://127.0.0.1:8000/test/

推荐答案

使用 redirect()快捷方式时,您实际上正在执行 HttpResponseRedirect(),因此需要将响应包含在您的 kwargs 中.

When using the redirect() shortcut you're actually doing a HttpResponseRedirect() and therefore need not to include the response in your kwargs.

此外,如果您想使用关键字参数进行重定向,则调用将为

Furthermore if you would like to redirect with keyworded arguments then the call would be

redirect('/myurl/', momma="im comin' home")

redirect('/myurl/', kwargs={'loads_a_kwargs':'cowboy'})

您收到的错误是因为您的正则表达式 url(r'^ demo/','demo',name ='demo') 接受任何参数.另外,通常您会使用 $ 结束所有url正则表达式,以表示捕获应停止.

The error you're getting is because your regexp url(r'^demo/', 'demo', name='demo') does not accept any parameters. Also, normally you would end all your url regexes with $ to denote that the capturing should stop.

这篇关于使用参数重定向.NoReverseMatch在/test/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆