title: django redirect after login not working "next" not posting?
body: <p>I have a login page which is working fine with the exception of the redirect to the referrer page. The user gets an email with a direct link within the app, they (in this example) are not logged in already and are redirected to the login page. After successful login the user is redirected to a hardcoded path. See example below.</p>
  
  <p>URL in email: <code>http://localhost:8000/issueapp/1628/view/22</code></p>
  
  <p>URL of login page: <code>http://localhost:8000/login?next=/issueapp/1628/view/22</code></p>
  
  <p>Login view (with hardcoded redirect):</p>
  
  <pre><code>def login_user(request):    
      state = "Please log in below..."
      username = password = ''
  
      if request.POST:
          username = request.POST['username']
          password = request.POST['password']
  
          user = authenticate(username=username, password=password)
          if user is not None:
              if user.is_active:
                  login(request, user)
                  state = "You're successfully logged in!"
                  return HttpResponseRedirect('/issueapp/1628/')
              else:
                  state = "Your account is not active, please contact the site admin."
          else:
              state = "Your username and/or password were incorrect."
  
      return render_to_response(
          'account_login.html',
          {
          'state':state,
          'username': username
          },
          context_instance=RequestContext(request)
      )
  </code></pre>
  
  <p>Login view (with "next" redirect):</p>
  
  <pre><code>def login_user(request):    
      state = "Please log in below..."
      username = password = ''
  
      if request.POST:
          username = request.POST['username']
          password = request.POST['password']
  
          user = authenticate(username=username, password=password)
          if user is not None:
              if user.is_active:
                  login(request, user)
                  state = "You're successfully logged in!"
                  return HttpResponseRedirect(request.GET['next'])
              else:
                  state = "Your account is not active, please contact the site admin."
          else:
              state = "Your username and/or password were incorrect."
  
      return render_to_response(
          'account_login.html',
          {
          'state':state,
          'username': username
          },
          context_instance=RequestContext(request)
      )
  </code></pre>
  
  <p>The above view results in an exception <code>"Key 'next' not found in &lt;QueryDict: {}&gt;"</code> The form does not appear to be posting the "next" variable, even though its there in the url and in the form. 
  I have searched and looked everywhere and cant figure out why its not working. Any ideas?</p>
  
  <p><strong>Additional reference:</strong></p>
  
  <p>Login template:</p>
  
  <pre><code>{% block content %}
  
      {{ state }}
      &lt;form action="/login/" method="post" &gt;
                  {% csrf_token %}
          {% if next %}
          &lt;input type="hidden" name="next" value="{{ next }}" /&gt;
          {% endif %}
          username:
          &lt;input type="text" name="username" value="{{ username }}" /&gt;&lt;br /&gt;
          password:
          &lt;input type="password" name="password" value="" /&gt;&lt;br /&gt;
  
          &lt;input type="submit" value="Log In"/&gt;
  
          {% debug %}
      &lt;/form&gt;
  {% endblock %}
  </code></pre>
  
  <p><em><strong></em> EDIT: The below is the code which is now working for me (thanks to the help of Paulo Bu)! **</strong></p>
  
  <p>Login View:</p>
  
  <pre><code>def login_user(request):
  
      state = "Please log in below..."
      username = password = ''
  
      next = ""
  
      if request.GET:  
          next = request.GET['next']
  
      if request.POST:
          username = request.POST['username']
          password = request.POST['password']
  
          user = authenticate(username=username, password=password)
          if user is not None:
              if user.is_active:
                  login(request, user)
                  state = "You're successfully logged in!"
                  if next == "":
                      return HttpResponseRedirect('/issueapp/')
                  else:
                      return HttpResponseRedirect(next)
              else:
                  state = "Your account is not active, please contact the site admin."
          else:
              state = "Your username and/or password were incorrect."
  
      return render_to_response(
          'account_login.html',
          {
          'state':state,
          'username': username,
          'next':next,
          },
          context_instance=RequestContext(request)
      )
  </code></pre>
  
  <p>Login Template:</p>
  
  <pre><code>{{ state }}
  
  {% if next %}
  &lt;form action="/login/?next={{next}}" method="post" &gt;
  {%else%}
  &lt;form action="/login/" method="post" &gt;
  {% endif %}
  
              {% csrf_token %}
  
      username:
      &lt;input type="text" name="username" value="{{ username }}" /&gt;&lt;br /&gt;
      password:
      &lt;input type="password" name="password" value="" /&gt;&lt;br /&gt;
  
      &lt;input type="submit" value="Log In"/&gt;
  
      {% debug %}
  &lt;/form&gt;
  </code></pre>