title: Redirect to previous page after registration in Django
body: <p>For login and logout, I use Django's built-in view in django.contrib.auth.views. It is very easy to redirect user to previous page because you can pass a 'next' parameter to the login/logout view. They work quit fine. </p>
  
  <p>When I want to fulfill the similar functionality for Registration, there are some problems. It seems that Django doesn't have a built-in registration view. So I'm using the CreateView with 'model=User'. Codes are following:</p>
  
  <pre><code>#views.py
  
  class Registration(CreateView):
      template_name='accounts/registration.html'
      model=User
  
      #Overwrite form_valid() to log this new user in after his successful registartion
      def form_valid(self, form):
          validation=super(Registration, self).form_valid(form)
  
          username=form.cleaned_data['username']
          password=form.cleaned_data['password2']
          user=authenticate(username=username, password=password)
          login(self.request, user)
  
          return validation
  </code></pre>
  
  <p>Now I don't have a similar 'next' parameter as in the built-in login/logout view. Then how can I redirect the new user to previous page after registration?</p>
  
  <p>I tried the following method:</p>
  
  <p><strong>Step1:</strong> 
  From the template which will trigger my Registration view, pass a 'next' parameter via GET method.</p>
  
  <pre><code>&lt;a href="{% url 'accounts:Registration' %}?next={{request.path}}"&gt;Registration&lt;/a&gt;
  </code></pre>
  
  <p>By the way, the url related to registration is:</p>
  
  <pre><code>#accounts/urls.py
  
  urlpatterns = patterns('',
      # Others
      url(r'^registration/$', views.Registration.as_view(), name='registration'),
  )
  </code></pre>
  
  <p><strong>Step2:</strong> 
  Overwrite the get_context_data() and get_success_url() methods, trying to construct my own 'next' parameter:</p>
  
  <pre><code>#views.py
  
      class Registration(CreateView):
  
          # other codes as above
  
          def get_context_data(self, **kwargs):
                context=super(Registration, self).get_context_data(**kwargs)
                context[next]=self.request.GET['next']
                return context
  
          def get_success_url(self):
                redirect_to=self.request.POST['next']
                return redirect_to
  </code></pre>
  
  <p><strong>Step3:</strong> 
  In my template, add a 'hidden' input field to hold the next value:</p>
  
  <pre><code># accounts/registration.html
  
  &lt;form method="post" action="{% url 'accounts:registration' %}" &gt; {% csrf_token %}
      &lt;p&gt;
      {{form.as_p}}
      &lt;/p&gt;
      &lt;input type="submit" value="Regsitarte" /&gt; 
      &lt;input type="hidden" name="next" value={{next}} /&gt; 
  &lt;/form&gt;
  </code></pre>
  
  <p>This method only works fine when the user doesn't make  any mistake during his registration procedure. If, for example, the user POSTs a username which is existing, or the format of the email address is invalid, this method will report an error about the "next" parameter.</p>
  
  <p>Can anyone give a method to redirect a user after registration? Or Can you alter my codes to fulfill this functionality? I'd like to use Django's Built-in Class-based Generic Views, instead of write a new view from scratch.  </p>