2012-04-03 54 views
1

我已經在幾個頁面中創建了登錄頁面強制登錄。現在,我需要在成功登錄後重新導向上一頁。登錄後重定向回到上一頁(Django)

當我強制登錄@login_required(login_url='/login/')。它包括查詢字符串中的next參數。

enter image description here

我試着用redirect_to = request.REQUEST.get('next', '')抓住它,並把它重定向return HttpResponseRedirect(redirect_to),但沒有奏效。

view.py

def login(request): 
    def errorHandle(error): 
     form = LoginForm() 
     return render_to_response('login/login.html', { 
      'error' : error, 
      'form' : form, 
      }) 
    if request.method == 'POST': # If the form has been submitted... 
     form = LoginForm(request.POST) # A form bound to the POST data 
     if form.is_valid(): # All validation rules pass 
      username = request.POST['username'] 
      password = request.POST['password'] 
      redirect_to = request.REQUEST.get('next', '') 
      user = authenticate(username=username, password=password) 
      if user is not None: 
       if user.is_active: 
        # Redirect to a success page. 
        auth_login(request, user) 
        return HttpResponseRedirect(redirect_to) 
#     return render_to_response('login/logged_in.html', { 
#      'username': username, 
#      }) 
       else: 
        # Return a 'disabled account' error message 
        error = u'Account Disabled' 
        return errorHandle(error) 
      else: 
      # Return an 'invalid login' error message. 
       error = u'Invalid Login' 
       return errorHandle(error) 
     else: 
      error = u'Form is Invalid' 
      return errorHandle(error) 
    else: 
     form = LoginForm() # An unbound form 
     return render_to_response('login/login.html', { 
      'form': form, 
      }) 

模板

<div id="login_form"> 
<form action="." method="post"> 
    {{ form.as_p }} 
    <input type="submit" value="Login"> 
</form> 
</div> 

編輯 - 我發現這個問題。這是我的模板問題。下面的表格工作正常。

<form action="" method="post"> 
     <label for="username">User name:</label> 
     <input type="text" name="username" value="" id="username"> 
     <label for="password">Password:</label> 
     <input type="password" name="password" value="" id="password"> 

     <input type="submit" value="Login" id="button"/> 
     <input type="hidden" name="next" value="{{ next|escape }}" /> 
    </form> 
+3

爲什麼不使用內置的[登錄](https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.views.login)視圖?爲什麼不至少[看看它的代碼](https://code.djangoproject.com/browser/django/trunk/django/contrib/auth/views.py#L25)看看它是如何進行重定向的? – DrTyrsa 2012-04-03 07:53:08

+0

@DTTyrsa我不知道如何使用內置登錄:/你可以發現這個代碼的問題? – ChamingaD 2012-04-03 08:14:37

+0

你有沒有閱讀[文檔](https://docs.djangoproject.com/en/dev/topics/auth/)auth?那裏有什麼不清楚的地方? – DrTyrsa 2012-04-03 08:28:15

回答

2

你可以只添加此窗體:

<input type="hidden" value="{% if request.REQUEST.next %}{{ request.REQUEST.next }}{% else %}{% if request.META.HTTP_REFERER %}{{ request.META.HTTP_REFERER }}{% else %}/{% endif %}{% endif %}" name="next" /> 

下面是它解決了案例:

  1. 如果用戶來自一個login_required視圖:接下來的設爲通過login_required
  2. 如果用戶來自登錄表格(即。認證未通過):下一個=預先設定的下一個值
  3. 如果用戶來自非login_required視圖:下一=引薦URL
  4. 如果用戶直接打開登錄頁面:下一=/

請注意,這也適用於您應該使用的外部登錄視圖。

作爲一個好東西,這裏是我的註銷模板的一部分,它將用戶重定向到註銷前的頁面。

{% block body %} 
    <p>{% trans "You've been logged out." %}</p> 
    {% if '/account/logout/' not in request.META.HTTP_REFERER %} 
    <p> 
     {% trans 'You will be redirected in a second' %} 
    </p> 
    {% endif %} 
{% endblock %} 

{% block extra_body %} 
    {% if '/account/logout/' not in request.META.HTTP_REFERER %} 
     <script type="text/javascript"> 
     document.location.href = '{{ request.META.HTTP_REFERER }}'; 
     </script> 
    {% endif %} 
{% endblock %} 

,我想感謝那些僱我sports pick, tipster social network偉大的啓動,因爲他們讓我爲0)共享代碼和1)利用我的時間去幫助別人。但是我把它鏈接起來的真正原因是你可以測試它。如果這個代碼不適合你喜歡它在那裏工作,那麼你有另一個問題。

+2

同樣的問題:重塑[輪子]有什麼意義(https://code.djangoproject.com/browser/django/trunk/django/contrib/auth/views.py#L25 )? – DrTyrsa 2012-04-03 08:28:52

+0

我猜教育目的是合理的。就我個人而言,多年來我一直沒有做過與登錄視圖相關的任何事情,一直忙於關注讓我的應用程序不同的原因......感謝Pinax提供包含所有這些樣板的入門項目。但之前,我曾經手動做過這方面的工作,這幫助我瞭解發生了什麼。 – jpic 2012-04-03 08:34:50

+2

我認爲,研究內置視圖的代碼對教育目的更有用。例如,可以瞭解有關測試Cookie和重定向安全檢查的信息。 – DrTyrsa 2012-04-03 08:41:01