Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django redirect to view

I have one view that I want to do some stuff in and then redirect to another view with a success message. The signature of the method that I want to redirect to is

quizView(request, quizNumber, errorMessage=None, successMessage=None):  

and my attempt at redirecting to that view looks like this:

return redirect(quizView, quizNumber=quizNumber, errorMessage=None, successMessage="Success!") 

I've tried most every combination of named and un-named paramaters and I can't get it to do what I want. Is there a way to make this work?

Also, I tried just returning the the second view with the first but then the URL is still where it was in the old view instead of appearing as though it has redirected.

Thanks!

like image 398
user1056805 Avatar asked Nov 11 '12 05:11

user1056805


People also ask

What is redirect view in Django?

In Django, you redirect the user to another URL by returning an instance of HttpResponseRedirect or HttpResponsePermanentRedirect from your view. The simplest way to do this is to use the function redirect() from the module django.shortcuts .

How do you redirect a class based view?

You should just name your urlpattern and redirect to that, that would be the most Django-ey way to do it. It's not documented (so not guaranteed to work in future Django versions) but the redirect shortcut method can take a view function, so you can almost do redirect(ClassView. as_view()) ...

How do I move one page to a different page in Django?

In Django, redirection is accomplished using the 'redirect' method. The 'redirect' method takes as argument: The URL you want to be redirected to as string A view's name. In the above example, first we imported redirect from django.


2 Answers

You haven't given your URL a name, so you need to use the whole path to the view function. Plus, that URL doesn't take errorMessage or successMessage parameters, so putting them into the reverse call will fail. This is what you want:

return redirect('quizzes.views.quizView', quizNumber=quizNumber) 
like image 163
Daniel Roseman Avatar answered Sep 22 '22 00:09

Daniel Roseman


Can you try example 2 given in https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect

  • Make sure that it does the 'reverse' on the given view.
  • Your view name should be wrapped in single quotes/
like image 20
Sivasubramaniam Arunachalam Avatar answered Sep 23 '22 00:09

Sivasubramaniam Arunachalam