title: Django: Redirect to previous page after login
body: <p>I'm trying to build a simple website with login functionality very similar to the one here on SO.
  The user should be able to browse the site as an anonymous user and there will be a login link on every page. When clicking on the login link the user will be taken to the login form. After a successful login the user should be taken back to the page from where he clicked the login link in the first place.
  I'm guessing that I have to somehow pass the url of the current page to the view that handles the login form but I can't really get it to work.</p>
  
  <p>EDIT:
  I figured it out. I linked to the login form by passing the current page as a GET parameter and then used 'next' to redirect to that page. Thanks!</p>
  
  <p>EDIT 2:
  My explanation did not seem to be clear so as requested here is my code:
  Lets say we are on a page foo.html and we are not logged in. Now we would like to have a link on foo.html that links to login.html. There we can login and are then redirected back to foo.html.
  The link on foo.html looks like this:</p>
  
  <pre><code>      &lt;a href='/login/?next={{ request.path }}'&gt;Login&lt;/a&gt; 
  </code></pre>
  
  <p>Now I wrote a custom login view that looks somewhat like this:</p>
  
  <pre><code>def login_view(request):
     redirect_to = request.REQUEST.get('next', '')
     if request.method=='POST':
        #create login form...
        if valid login credentials have been entered:
           return HttpResponseRedirect(redirect_to)  
     #...
     return render_to_response('login.html', locals())
  </code></pre>
  
  <p>And the important line in login.html:</p>
  
  <pre><code>&lt;form method="post" action="./?next={{ redirect_to }}"&gt;
  </code></pre>
  
  <p>So yeah thats pretty much it, hope that makes it clear.</p>