2012-12-14 46 views
5

目前我测试的Django 1.5和它的自定义用户模型,但我已经有些了解的问题 我在我的帐户的应用程序,它看起来创建一个User类,如:身份验证和Django 1.5

class User(AbstractBaseUser): 
    email = models.EmailField() 
    activation_key = models.CharField(max_length=255) 
    is_active = models.BooleanField(default=False) 
    is_admin = models.BooleanField(default=False) 

    USERNAME_FIELD = 'email' 

我可以正确地注册存储在我的account_user表中的用户。 现在,我该如何登录? 我试着

def login(request): 
    form = AuthenticationForm() 
    if request.method == 'POST': 
     form = AuthenticationForm(request.POST) 
     email = request.POST['username'] 
     password = request.POST['password'] 
     user = authenticate(username=email, password=password) 
     if user is not None: 
      if user.is_active: 
       login(user) 
      else: 
       message = 'disabled account, check validation email' 
       return render(
         request, 
         'account-login-failed.html', 
         {'message': message} 
       ) 
    return render(request, 'account-login.html', {'form': form}) 

,但用户是不渲染它的登录表单:( 为什么我autheticate返回我什么事? 任何想法?

forms.py

class RegisterForm(forms.ModelForm): 
    """ a form to create user""" 
    password = forms.CharField(
      label="Password", 
      widget=forms.PasswordInput() 
    ) 
    password_confirm = forms.CharField(
      label="Password Repeat", 
      widget=forms.PasswordInput() 
    ) 
    class Meta: 
     model = User 
     exclude = ('last_login', 'activation_key') 

    def clean_password_confirm(self): 
     password = self.cleaned_data.get("password") 
     password_confirm = self.cleaned_data.get("password_confirm") 
     if password and password_confirm and password != password_confirm: 
      raise forms.ValidationError("Password don't math") 
     return password_confirm 

    def clean_email(self): 
     if User.objects.filter(email__iexact=self.cleaned_data.get("email")): 
      raise forms.ValidationError("email already exists") 
     return self.cleaned_data['email'] 

    def save(self): 
     user = super(RegisterForm, self).save(commit=False) 
     user.password = self.cleaned_data['password'] 
     user.activation_key = generate_sha1(user.email) 
     user.save() 

     return user 
+0

什么是你期待你打电话'登录()'新用户之后会发生什么? –

+0

这不是问题,我打开一个会话渲染()。 – billyJoe

+0

它完全*是*的问题。你呼叫登录,然后......什么都不是。您只需拖放到最后一行,再次呈现登录表单。如果你想做其他事情,你需要实际上放一些代码来做其他事情。 –

回答

9

Django documentation有一个很好的使用新的自定义用户的例子。

从你的代码t他唯一看到缺少的是自定义身份验证后端。

我有一个名为auth.py的文件。 “authenticate”和“get_user”方法是必需的。

from models import User as CustomUser 

class CustomAuth(object): 

    def authenticate(self, username=None, password=None): 
     try: 
      user = CustomUser.objects.get(email=username) 
      if user.check_password(password): 
       return user 
     except CustomUser.DoesNotExist: 
      return None 

    def get_user(self, user_id): 
     try: 
      user = CustomUser.objects.get(pk=user_id) 
      if user.is_active: 
       return user 
      return None 
     except CustomUser.DoesNotExist: 
      return None 

则认证后端必须指定在你的设置文件

AUTHENTICATION_BACKENDS = ('apps.accounts.auth.CustomAuth') 
+0

伟大的这是我错过的,非常感谢 – billyJoe

+1

如果你使用AbstractBaseUser,是否有必要创建一个自定义认证b ackend?我们还不能使用'django.contrib.auth.authenticate' [[code](https://github.com/django/django/blob/master/django/contrib/auth/backends.py#L11) )],因为它依赖于'settings.AUTH_USER_MODEL'?我没有看到这个版本做了什么不同于那个。可以? –

+2

你可能想要继承'ModelBackend':'class CustomAuth(ModelBackend)' – AJJ