2012-10-30 113 views
3

我只是試圖運行一個簡單的{% if user.is_authenticated %}。但它總是返回FalseDjango if user.is_authenticated not working

這是我的所有文件。

views.py

from django.shortcuts import render_to_response, redirect 
from django.core.urlresolvers import reverse 
from django.template import RequestContext 
from django.contrib.auth.models import User 
from django.http import HttpResponse 
from django.contrib.auth.hashers import * 
from forms import UserLoginForm 
from models import UserLogin 

def index(request): 
    return render_to_response('index.html', context_instance = RequestContext(request)) 

def login(request): 
    if request.method == 'POST': 
     form = UserLoginForm(request.POST or None) 
     if form.is_valid(): 
      email_from_form = form.cleaned_data['email'] 
      password_from_form = form.cleaned_data['password'] 
      users = User.objects.filter(email = email_from_form) 

      for j in users: 
       if j.email == email_from_form: 
        pass_match = check_password(password_from_form, j.password) 
        if pass_match: 
         return redirect(reverse('profile'), context_instance = RequestContext(request)) 
        else: 
         return HttpResponse('Entered password did not match') 
       else: 
        return HttpResponse('Entered email does not exist') 
     else: 
      return HttpResponse(form.errors) 
    else: 
     form = UserLoginForm() 

    return render_to_response('login.html', {'form':form}, context_instance = RequestContext(request)) 


def profile(request): 

    return render_to_response('profile.html', context_instance = RequestContext(request)) 

forms.py

from django import forms 

class UserLoginForm(forms.Form): 
    email = forms.EmailField(max_length = 100) 
    password = forms.CharField(max_length = 100) 

models.py

from django.db import models 

class UserLogin(models.Model): 
    email = models.EmailField(max_length = 100) 
    password = models.CharField(max_length = 100) 

    def __unicode__(self): 
     return self.email 

profile.html

<html> 
<head> 
    <title>profile</title> 
</head> 
<body> 
    {% if user.is_authenticated %} 
     Welcome user, you are loged in.. 
    {% else %} 
     You are not logged in 
    {% endif %} 
</body> 
</html> 

login.html

<html> 
<head> 
    <title>login</title> 
</head> 
<body> 
    <form action="{% url login %}" method="POST"> 
     {% csrf_token %} 
     {{ form.as_p }} 
     <input type="submit" id="submit" name="submit" value="Login" /> 
    </form> 

</body> 
</html> 

它總是返回You are not logged in

我是新來的Django,我不明白這是爲什麼呢就是這個樣子。

+0

請注意'user.is_authenticated'是一種方法,而不是屬性。因此,你必須像這樣檢查:'user.is_authenticated()' https://docs.djangoproject.com/en/1.9/ref/contrib/auth/#django.contrib.auth.models.User.is_authenticated – TanguyP

回答

8

你永遠不記錄用戶在嘗試大意如下的東西:

from django.contrib.auth import authenticate, login as auth_login 

if request.method == 'POST': 
    form = UserLoginForm(request.POST or None) 
    if form.is_valid(): 
     username = User.objects.get(email=form.cleaned_data['email']) 
     password = form.cleaned_data['password'] 
     user = authenticate(username=username, password=password) 
     if user: 
      if user.is_active: 
       auth_login(request, user) 
       return HttpResponseRedirect(request.GET.get('next', 
              settings.LOGIN_REDIRECT_URL)) 
     else: 
      error = 'Invalid username or password.' 

更新使用您的形式清晰;排除錯誤檢查。

+0

我們是否總是需要'用戶名'來驗證?而不是'emailid'? –

+0

您可以使用電子郵件ID作爲用戶名 –

+1

請注意,用戶名的默認長度爲30個字符,在某些情況下,對於電子郵件地址可能太短。我建議看看自定義的後端,而不是這個:http://djangosnippets.org/snippets/74/。同時檢查註釋,因爲如果您想使用該字段來存儲電子郵件地址,您可能必須手動擴展用戶名字段的字段長度 – Mikael