2012-08-16 79 views
0

views.py

def login_user(request): 
    if request.POST: 
     sform = LoginForm(request.POST) 
     rform = UserForm() 
     if sform.is_valid(): 
      username = request.POST.get('username') 
      password = request.POST.get('password') 
      user = authenticate(username = username,password = password) 
      if user is not None: 
       if user.is_active: 
        login(request,user) 
        return redirect(request.path) 
       else: 
        state = "Your account is not active." 
        #or first set the user to is active and then log in 
        return redirect('acitvate_account',user = user) 
       return render_to_response('home.html',{'state':state,'user':user},context_instance = RequestContext(request)) 
      else: 
       state = "Incorrect Username or Password" 
       #sform = LoginForm() 
       form = LoginForm() 
       return render_to_response('register.html',{'state':state,'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request)) 
     else: 
      state = "Invalid form" 
      dLoginForm = LoginForm() 
      rform = PartialSignupForm() 
      return render_to_response('register.html',{'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request)) 
    else: 
     dLoginForm = LoginForm() 
     rform = PartialSignupForm() 
     return render_to_response('category.html',{'dLoginForm':dLoginForm,'rform':rform},context_instance = RequestContext(request)) 

urls.py

urlpatterns += patterns('beenthere.views', 
    #beenthere urls 
    (r'^$','index'), 
    (r'^home2/$','category'), 
    (r'^accounts/login/$','login_user'), 
    (r'^accounts/register/$','register_user'), 
    (r'^home/(?P<user>\w*)$','home'), 
    (r'^logout/','logout_user'), 

    ) 

疑問

我怎麼想重定向到相同的URL從哪個請求發送,我嘗試使用request.path,但它將我重定向到/ accounts/login/url,這是不希望的。重定向到從請求發送相同的URL

回答

2

你需要做的是什麼跟蹤引薦是當你顯示的登錄表單,可能在存儲的用戶會話的值,然後將用戶重定向回該網址後,他們已經成功登錄。

沿線的

東西:

def login_user(request): 
    if request.POST: 
     sform = LoginForm(request.POST) 
     rform = UserForm() 
     if sform.is_valid(): 
      username = request.POST.get('username') 
      password = request.POST.get('password') 
      user = authenticate(username = username,password = password) 
      if user is not None: 
       if user.is_active: 
        login(request,user) 
        return redirect(request.path) 
       else: 
        state = "Your account is not active." 
        #or first set the user to is active and then log in 
        return redirect(req.session['after_login_url'],user = user) 
       return render_to_response('home.html',{'state':state,'user':user},context_instance = RequestContext(request)) 
      else: 
       state = "Incorrect Username or Password" 
       #sform = LoginForm() 
       form = LoginForm() 
       return render_to_response('register.html',{'state':state,'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request)) 
     else: 
      state = "Invalid form" 
      dLoginForm = LoginForm() 
      rform = PartialSignupForm() 
      return render_to_response('register.html',{'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request)) 
    else: 
     dLoginForm = LoginForm() 
     rform = PartialSignupForm() 
     req.session['after_login_url'] = req.META.get('HTTP_REFERER') 
     return render_to_response('category.html',{'dLoginForm':dLoginForm,'rform':rform},context_instance = RequestContext(request)) 

當然,你也應該補充檢查,以確保該HTTP_REFERER頭存在,並確保after_login_url鍵存在(並可能有一個健全的價值,如果外部網站直接鏈接到您的登錄頁面或類似鏈接),並且如果有任何這些測試失敗,請安全回退l(就像用戶的個人資料頁面或您的網站索引頁面一樣)。

+0

感謝喬,但沒有任何簡單的解決方案,如果我直接使用request.META.get('HTTP_REFERER')重定向 – Abhimanyu 2012-08-16 14:56:44

+0

當提交表單時HTTP_REFERER將會是登錄表單本身。 HTTP_REFERER是用戶在加載當前頁面時來自的URL,因此如果您想在提交表單後使用它來重定向它們,則必須在最初查看錶單時抓取它。 – 2012-08-16 14:58:44

+0

是啊是的,我已經得到了你的觀點,但我在同一頁面上使用登錄表單和註冊表單,這些是對話框形式,所以它是一種動態的,所以HTTP_REFERER是我希望它重定向的相同url,所以它工作在我的情況,但你的解決方案是更廣泛的,謝謝 – Abhimanyu 2012-08-16 15:05:43