2013-11-22 44 views
0

我有點新的django和工作的用戶處理。 現在我已經整理了一切,它的工作原理很好,除了當用戶輸入無法登錄的數據時。 未顯示錯誤消息。 我想知道是否會有一個更容易/更好的方法來解決這個問題,然後把我的所有視圖中的空變量,除了無效登錄。 (存儲在消息)django重定向變量索引

我auth_view:

def auth_view(request): 
    username = request.POST.get('username', '') 
    password = request.POST.get('password', '') 

    #returns None if not correct 
    user = auth.authenticate(username=username, password=password) 

    if user is not None: 
     auth.login(request, user) 
     return HttpResponseRedirect(reverse('index')) 
    else: 
     invalid_login = "ErrorMessage" 
     return HttpResponseRedirect(reverse('index') 

指數就是在這一刻:

def index(request): 
    return render(request, 'index.html') 

我怎麼能解決這個問題? (或者你會建議我只是到處都是多餘的VAR接近它)

隨着親切的問候 漢斯

+0

的UserAuthenticationForm一個登錄表單,一些驗證也許是爲了? –

回答

0

這是我如何解決它:

def index(request): 
    """Logs a user into the application.""" 
    auth_form = AuthenticationForm(None, request.POST or None) 

    # The form itself handles authentication and checking to make sure passowrd and such are supplied. 
    if auth_form.is_valid(): 
     login(request, auth_form.get_user()) 
     return HttpResponseRedirect(reverse('index')) 

    return render(request, 'index.html', {'auth_form': auth_form} 

現在的問題是,從CSS我的風格類似乎沒有工作了。

但還有就快了

(這裏使用