Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django return redirect() with parameters

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): 
like image 440
muntu Avatar asked Jul 09 '10 04:07

muntu


People also ask

How do you pass arguments in Django redirect?

shortcuts and for redirection to the Django official website we just pass the full URL to the 'redirect' method as string, and for the second example (the viewArticle view) the 'redirect' method takes the view name and his parameters as arguments.

Can we pass data with redirect in Django?

You can now perform a redirect with Django, either by using the redirect response classes HttpResponseRedirect and HttpResponsePermanentRedirect , or with the convenience function django. shortcuts. redirect() .

What is the difference between HttpResponseRedirect and redirect?

There is a difference between the two: In the case of HttpResponseRedirect the first argument can only be a url . redirect which will ultimately return a HttpResponseRedirect can accept a model , view , or url as it's "to" argument. So it is a little more flexible in what it can "redirect" to.

What is the difference between render and redirect in Django?

The render function Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text. You request a page and the render function returns it. The redirect function sends another request to the given url.


1 Answers

urls.py:

#...     url(r'element/update/(?P<pk>\d+)/$', 'element.views.element_update', name='element_update'), 

views.py:

from django.shortcuts import redirect from .models import Element   def element_info(request):     # ...     element = Element.object.get(pk=1)     return redirect('element_update', pk=element.id)  def element_update(request, pk)     # ... 
like image 59
sergi0 Avatar answered Oct 21 '22 21:10

sergi0