Django return redirect() with parameters

Question:

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):
Asked By: muntu

||

Answers:

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
Answered By: Daniel Roseman

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)
    # ...
Answered By: sergi0

I do like this in django3

redirect_url = reverse('my_function', args=(backend,))
parameters = urlencode(form.cleaned_data)
return redirect(f'{redirect_url}?{parameters}')
Answered By: yanqzhi

I am new to Django. One of my project, I used render instead of redirect to send data. That worked good. My code was like this —>

  for key, value in request.POST.lists():
        print(key, value)
        if key.split('-')[-1] != 'csrfmiddlewaretoken':
            qu_id = key.split('-')[-1]
            get_answer = Answer.objects.filter(question_id=qu_id,
                                               correct_answer__option__contains=value[0])
            total_correct_answer = get_answer.count()
    context = {'score': total_correct_answer}
    return render(request, 'result.html', context)
context = {'questions': questions, 'total_correct_answer': total_correct_answer}
return render(request, 'test.html', context)
Answered By: Rafsan
def someview(request, *args, **kwargs):
    pass
    #your code

# insted of redirect return the view as sach

return someview(request, my_args, my_kwargs=kwargs) 
Answered By: yoyoedv