Django:如何编写以下的反向函数
urlconf 和视图如下:
url(r'^register/$',
register,
{ 'backend': 'registration.backends.default.DefaultBackend' },
name='registration_register'),
def register(request, backend, success_url=None, form_class=None,
disallowed_url='registration_disallowed',
template_name='registration/registration_form.html',
extra_context=None):
我想要做的是将用户重定向到注册页面并指定 success_url。我尝试了反向('registration.views.register', kwargs={'success_url':'/test/' }) 但这似乎不起作用。我已经尝试了好几个小时了,却无法集中注意力。谢谢
The urlconf and view is as follows:
url(r'^register/
What i want to do is redirect users to the register page and specify a success_url. I tried reverse('registration.views.register', kwargs={'success_url':'/test/' }) but that doesn't seem to work. I've been trying for hours and can't get my mind around getting it right. Thanks
,
register,
{ 'backend': 'registration.backends.default.DefaultBackend' },
name='registration_register'),
def register(request, backend, success_url=None, form_class=None,
disallowed_url='registration_disallowed',
template_name='registration/registration_form.html',
extra_context=None):
What i want to do is redirect users to the register page and specify a success_url. I tried reverse('registration.views.register', kwargs={'success_url':'/test/' }) but that doesn't seem to work. I've been trying for hours and can't get my mind around getting it right. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望能够使用参数指定 reverse(),则必须在 URL 配置本身(正则表达式)中定义这些参数。例如:
编辑:
, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:
您可以将该 URL 部分包装在 ()? 中以使其可选(这样它也只匹配简单的 register/ )
args 和 kwargs 之间的区别在于,使用 args 可以指定未命名/命名的 URL 参数,而使用 kwargs 只能指定命名。
所以:
编辑:
, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:
您可以将该 URL 部分包装在 ()? 中以使其可选(这样它也只匹配简单的 register/ )
args 和 kwargs 之间的区别在于,使用 args 可以指定未命名/命名的 URL 参数,而使用 kwargs 只能指定命名。
所以:
编辑:
reverse('url_name', args=[my_success_url]) reverse('url_name', kwargs={'success_url': my_success_url}) // both work and do the same r'^register/(\w+)/对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:
编辑:
, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:
您可以将该 URL 部分包装在 ()? 中以使其可选(这样它也只匹配简单的 register/ )
args 和 kwargs 之间的区别在于,使用 args 可以指定未命名/命名的 URL 参数,而使用 kwargs 只能指定命名。
所以:
编辑:
reverse('url_name', args=[my_success_url]) // only this works对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:
编辑:
, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:
您可以将该 URL 部分包装在 ()? 中以使其可选(这样它也只匹配简单的 register/ )
args 和 kwargs 之间的区别在于,使用 args 可以指定未命名/命名的 URL 参数,而使用 kwargs 只能指定命名。
所以:
编辑:
对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:
If you want to be able to specify reverse() with parameters, those parameters have to be defined in the URL configuration itself (regexp). Something like:
Edit:
, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),For success_url params, if you want to be able to match any full relative URL, including possible GET params in the relative URL, the actual regexp could get pretty complex.
Something like (untested):
You can wrap that URL section in ()? to make it optional (So that it matches just simple register/ too)
The difference between args and kwargs is that with args you can specify unnamed/named URL params while with kwargs only named.
So:
Edit:
, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),For success_url params, if you want to be able to match any full relative URL, including possible GET params in the relative URL, the actual regexp could get pretty complex.
Something like (untested):
You can wrap that URL section in ()? to make it optional (So that it matches just simple register/ too)
The difference between args and kwargs is that with args you can specify unnamed/named URL params while with kwargs only named.
So:
Edit:
reverse('url_name', args=[my_success_url]) reverse('url_name', kwargs={'success_url': my_success_url}) // both work and do the same r'^register/(\w+)/For success_url params, if you want to be able to match any full relative URL, including possible GET params in the relative URL, the actual regexp could get pretty complex.
Something like (untested):
Edit:
, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),For success_url params, if you want to be able to match any full relative URL, including possible GET params in the relative URL, the actual regexp could get pretty complex.
Something like (untested):
You can wrap that URL section in ()? to make it optional (So that it matches just simple register/ too)
The difference between args and kwargs is that with args you can specify unnamed/named URL params while with kwargs only named.
So:
Edit:
reverse('url_name', args=[my_success_url]) // only this worksFor success_url params, if you want to be able to match any full relative URL, including possible GET params in the relative URL, the actual regexp could get pretty complex.
Something like (untested):
Edit:
, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),For success_url params, if you want to be able to match any full relative URL, including possible GET params in the relative URL, the actual regexp could get pretty complex.
Something like (untested):
You can wrap that URL section in ()? to make it optional (So that it matches just simple register/ too)
The difference between args and kwargs is that with args you can specify unnamed/named URL params while with kwargs only named.
So:
Edit:
For success_url params, if you want to be able to match any full relative URL, including possible GET params in the relative URL, the actual regexp could get pretty complex.
Something like (untested):
编辑:抱歉,完全误读了问题 - 我没有查看函数定义。实际上,这里的问题是您的 URLconf 的设计方式使得无法动态设置 success_url。它必须通过
extra_context
字典显式传递给函数 - 即您当前定义的backend
字典。由于 URL 本身没有任何内容可以接受此参数,因此必须对其进行硬编码。Edit: Sorry, completely misread the question - I didn't look at the function definition. Actually, the issue here is that your URLconf is designed in such a way as to make it impossible to set the success_url dynamically. It has to be passed explicitly to the function via the
extra_context
dictionary - ie the one where you have currently definedbackend
. Since there is nothing in the URL itself to accept this parameter, it has to be hard-coded there.