Django:如何编写以下的反向函数

发布于 2024-09-04 18:02:13 字数 554 浏览 12 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-09-11 18:02:13

如果您希望能够使用参数指定 reverse(),则必须在 URL 配置本身(正则表达式)中定义这些参数。例如:

url(r'^register/(?P<success_url>[\w\/]+)/

您可以将该 URL 部分包装在 ()? 中以使其可选(这样它也只匹配简单的 register/ )
argskwargs 之间的区别在于,使用 args 可以指定未命名/命名的 URL 参数,而使用 kwargs 只能指定命名。
所以:

r'^register/(?P<success_url>\w+)/

编辑
对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:

r'^register/(?P<success_url>[\w\/]+(((\?)([a-zA-Z]*=\w*)){1}((&)([a-zA-Z]*=\w*))*)?)/
,
    register,
    { 'backend': 'registration.backends.default.DefaultBackend' },
    name='registration_register'),

您可以将该 URL 部分包装在 ()? 中以使其可选(这样它也只匹配简单的 register/ )
argskwargs 之间的区别在于,使用 args 可以指定未命名/命名的 URL 参数,而使用 kwargs 只能指定命名。
所以:


编辑
对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:



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

您可以将该 URL 部分包装在 ()? 中以使其可选(这样它也只匹配简单的 register/ )
argskwargs 之间的区别在于,使用 args 可以指定未命名/命名的 URL 参数,而使用 kwargs 只能指定命名。
所以:


编辑
对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:



reverse('url_name', args=[my_success_url]) // only this works

编辑
对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:

, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),

您可以将该 URL 部分包装在 ()? 中以使其可选(这样它也只匹配简单的 register/ )
argskwargs 之间的区别在于,使用 args 可以指定未命名/命名的 URL 参数,而使用 kwargs 只能指定命名。
所以:

编辑
对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:

, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),

您可以将该 URL 部分包装在 ()? 中以使其可选(这样它也只匹配简单的 register/ )
argskwargs 之间的区别在于,使用 args 可以指定未命名/命名的 URL 参数,而使用 kwargs 只能指定命名。
所以:

编辑
对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:

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

您可以将该 URL 部分包装在 ()? 中以使其可选(这样它也只匹配简单的 register/ )
argskwargs 之间的区别在于,使用 args 可以指定未命名/命名的 URL 参数,而使用 kwargs 只能指定命名。
所以:

编辑
对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:

reverse('url_name', args=[my_success_url]) // only this works

编辑
对于 success_url 参数,如果您希望能够匹配任何完整的相对 URL(包括相对 URL 中可能的 GET 参数),则实际的正则表达式可能会变得相当复杂。
像(未经测试)的东西:

, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),

您可以将该 URL 部分包装在 ()? 中以使其可选(这样它也只匹配简单的 register/ )
argskwargs 之间的区别在于,使用 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:

url(r'^register/(?P<success_url>[\w\/]+)/

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:

r'^register/(?P<success_url>\w+)/

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):

r'^register/(?P<success_url>[\w\/]+(((\?)([a-zA-Z]*=\w*)){1}((&)([a-zA-Z]*=\w*))*)?)/
,
    register,
    { 'backend': 'registration.backends.default.DefaultBackend' },
    name='registration_register'),

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):



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+)/

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):


,
    register,
    { 'backend': 'registration.backends.default.DefaultBackend' },
    name='registration_register'),

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):



reverse('url_name', args=[my_success_url]) // only this works

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):

, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),

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):

, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),

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):

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+)/

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):

, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),

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):

reverse('url_name', args=[my_success_url]) // only this works

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):

, register, { 'backend': 'registration.backends.default.DefaultBackend' }, name='registration_register'),

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):

活雷疯 2024-09-11 18:02:13

编辑:抱歉,完全误读了问题 - 我没有查看函数定义。实际上,这里的问题是您的 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 defined backend. Since there is nothing in the URL itself to accept this parameter, it has to be hard-coded there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文