2017-01-05 10 views
0

render_to_responseについて言及している古くからの投稿がたくさんあります。私の理解から、redirect()はコンテキストを維持する必要があります。ユーザーはログインページで認証されますが、リダイレクトされたページでは認証されていないと表示されます

は、ここに私のログインビュー

def login(request): 

    context = {} 
    context['form'] = LoginForm 
    context['loginFailure'] = False 

    if request.POST: 
     username = request.POST['user'] 
     password = request.POST['password'] 

     user = authenticate(username=username, password=password) 

     if user is not None: 
      if user.is_authenticated: 
       print('logged in on /login!') 
       return redirect('/') 
     else:    
      context['loginFailure'] = True 
      print('Invalid login on /login!') 
      return render(request,'q/login.html', context)  

    return render(request,'q/login.html', context) 

私は有効な資格情報を使用してログインする場合それは正しく端子に出力するためのコードです。インデックスビューで

、私はこのコード

if request.user.is_authenticated: 
    print("index: Authenticated!") 
else: 
    print('index: Not authenticated...') 

を持っている。しかし、すべて私が正常にログインした時間及び/ログイン/成功のメッセージを出力し、インデックス・ビューは、私が認証されていないよと言われます。ログインに成功した後でコンテキストを維持するために必要なことは何ですか?

答えて

0

また、djangoログインを呼び出す必要があります。 doc ref

from django.contrib.auth import authenticate, login 
if user is not None: 
    login(request, user) 
    print('logged in on /login!') 
    return redirect('/') 
関連する問題