views:

53

answers:

1

In my view function I want to call another view and pass data to it :

return redirect('some-view-name', backend, form.cleaned_data)

, where backend is of registration.backends object, and form.cleaned_data is a dict of form data (but both must be either sent as *args or **kwargs to prevent raising Don't mix *args and **kwargs in call to reverse()! error). From what I've found in the docs :

def my_view(request):
    ...
    return redirect('some-view-name', foo='bar')

It looks like I need to provide 'some-view-name' argument, but is it just the name of the view function, or the name of the url ? So I would like to make it similar to the way it's done in django-registration, where :

to, args, kwargs = backend.post_registration_redirect(request, new_user)
return redirect(to, *args, **kwargs)

def post_registration_redirect(self, request, user):
    return ('registration_complete', (), {})

Ok so now, can I call directly my view function or do I need to provide a url for it ? And what more important, how my funciotn call (and a url if needed) should look like ? Both backend, and cleaned_data are just passed through this view for a later usage. I've tried this, but it's improper :

url(r'^link/$', some-view-name)   
def some-view-name(request, *args):

As well as this :

return redirect('some_url', backend=backend, dataform.cleaned_data) 
url(r'^link/$', some-view-name)    
def some-view-name(request, backend, data):

still NoReverseMatch . But in django-registration, I've seen something like this :

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='user/login_logout_register/registration_form.html',
             extra_context=None):
+1  A: 

Firstly, your URL definition does not accept any parameters at all. If you want parameters to be passed from the URL into the view, you need to define them in the urlconf.

Secondly, it's not at all clear what you are expecting to happen to the cleaned_data dictionary. Don't forget you can't redirect to a POST - this is a limitation of HTTP, not Django - so your cleaned_data either needs to be a URL parameter (horrible) or, slightly better, a series of GET parameters - so the URL would be in the form:

/link/mybackend/?field1=value1&field2=value2&field3=value3

and so on. In this case, field1, field2 and field3 are not included in the URLconf definition - they are available in the view via request.GET.

So your urlconf would be:

url(r'^link/(?P<backend>\w+?)/$', my_function)

and the view would look like:

def my_function(request, backend):
   data = request.GET

and the reverse would be (after importing urllib):

return "%s?%s" % (redirect('my_function', args=(backend,)),
                  urllib.urlencode(form.cleaned_data))

Edited after comment

The whole point of using redirect and reverse, as you have been doing, is that you go to the URL - it returns an Http code that causes the browser to redirect to the new URL, and call that.

If you simply want to call the view from within your code, just do it directly - no need to use reverse at all.

That said, if all you want to do is store the data, then just put it in the session:

request.session['temp_data'] = form.cleaned_data
Daniel Roseman
and if I won't operate on cleaned_data in this view, but just pass it for later usage ? I have many fields in the cleaned_data dict so I would like to avoid passing them as a get string :)
muntu
I don't understand this comment. Please explain in more detail, updating your question if necessary.
Daniel Roseman
this second view will only store this sent data for further usage. But are you sure I need to provide url for it ? From docs it looks like I'm just calling the view directly. Also I was hoping to just send a dictionary with backend and data in redirect() (as it is done in django-registration) and then in the url (like this dict in register function), but from what I see it's impossible ?
muntu
yes yes yes that's it !! I've wasted so much time on it completely forgetting about sessions :/ damn, thanks !!
muntu