views:

109

answers:

2

The urlconf and view is as follows:

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

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

A: 

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.

Daniel Roseman
So 'success_url', 'form_class', 'disallowed_url' etc. are all GET parameters? So in general, all kwargs set in a view function will be GET parameters?
ninja123
No probs, so you mean I cannot call reverse? but call the function directly? Can you please give me an example. Thanks
ninja123
They are not GET parameters, they are simply extra params sent to the Python view function. Params defined (more exactly 'captured') from the URL conf can be also GET params but not necessarily. Only if you choose to define the URL in such format : ^register?my_get_param=(?P<param>\w+)
Béres Botond
+1  A: 

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\/]+)/$',
                       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:

r'^register/(?P<success_url>\w+)/$'
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+)/$'
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):

r'^register/(?P<success_url>[\w\/]+(((\?)([a-zA-Z]*=\w*)){1}((&)([a-zA-Z]*=\w*))*)?)/$'
Béres Botond
Thanks a lot! The whole reason of this question is coz I want to redirect users to where they came from after they register. Guess I can use a session parameter as well. I find that I store session data sometimes as an easy way out. Would you recommend doing that in this scenario? Thanks again!
ninja123