2013-10-25 88 views
0

我爲登錄表單製作了一個包含標記,它將顯示在每個頁面中。 但是我無法在成功登錄後重定向頁面。包含標記中的HttpResponseRedirect

這裏是我的代碼:

from django import template 
from django.contrib.auth import authenticate, login 
from django.http.response import HttpResponseRedirect 

register = template.Library() 

def costumlogin(context): 
    request = context['request'] 
    if request.user.is_anonymous(): 
     state = "Please log in below..." 
     username = password = '' 
     if 'mylogin' in request.POST: 
      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) 
        state = "You're successfully logged in!" 
        return HttpResponseRedirect(request.META.get('HTTP_REFERER','/')) 
       else: 
        state = "Your account is not active, please contact the site admin." 
      else: 
       state = "Your username and/or password were incorrect." 
     return {'username':username, "state": state} 

register.inclusion_tag('registration/login.html',takes_context = True)(costumlogin) 

回答

1

你不能做到這一點,是沒有意義的。只有視圖才能返回響應,包括重定向。

模板標籤不適合處理表單。您應該將表單發佈到其他網址並在該視圖中進行處理。