2011-03-29 200 views
0

即時通訊沒有得到確切..實際上在httpresponse重定向它什麼也沒有顯示。 實際上http://127.0.0.1:8000/login/在顯示我的登錄頁面 現在我想正確登錄它將重定向到我的索引頁面。 不確切的方式。django頁面重定向成功登錄

def login(request): 
    template = "../templates/admin/login.html" 
    data = { 
     } 
    user = auth.authenticate(username='aa', password='bb') 
    if user is not None and user.is_active: 
     template = "../templates/admin/index.html" 

     auth.login(request, user) 
    return HttpResponseRedirect("/login/index/") 

    return render_to_response(template, data, 
           context_instance = RequestContext(request)) 

thanx提前。

+0

http://docs.djangoproject.com/en/1.3/topics/auth/#django.contrib.auth.login – 2011-03-29 13:17:56

+0

看起來是對的,我(假定壓痕是在你的版本是正確的)。那會拋出一個python錯誤,無論如何重定向到'/ login/index /',或者重定向成功。你說它正在返回一個空白頁面? – 2011-03-29 13:28:06

+0

實際上,當我寫http://127.0.0.1:8000/login/它會自動重定向到索引頁面,而不顯示登錄頁面 – gurr 2011-03-29 13:39:27

回答

1

如果您正在自動重定向,那麼您的縮進是關閉的。你有不幸的,但是你寫的代碼沒有觸發IndentationError

我的猜測是你從文檔複製並粘貼並添加到代碼中?

我會檢查一下,確保沒有標籤和空格混淆。

這是一個固定的,從POST請求中提取信息。

def login(request): 
    template = "../templates/admin/login.html" 
    data = {} 
    if request.method == 'POST': 
     user = auth.authenticate(username=request.POST.get('username'), 
              password=request.POST.get('password')) 
     if user is not None and user.is_active: 
      auth.login(request, user) 
      return HttpResponseRedirect("/login/index/") 
    return render_to_response(template, data, context_instance = RequestContext(request)) 
+0

不工作stilldef登錄(請求): template =「../templates/admin/login html的」 數據= { } 用戶= auth.authenticate(用戶名= 'AA',口令= 'B-B') 如果用戶不是無和user.is_active: auth.login(請求用戶) 返回HttpResponseRedirect(「/ login/index /」) return render_to_response(template,data, context_instance = RequestContext(request)) – gurr 2011-03-29 14:42:15

+0

哦,沒錯。您正在使用硬編碼值對用戶進行身份驗證,因此如果用戶'aa'的密碼爲'bb',您將按照您的設計獲得重定向。現在,該從request.POST提取用戶名和密碼了。 – 2011-03-29 16:18:03

相關問題