目前我測試的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
什麼是你期待你打電話'登錄()'新用戶之後會發生什麼? –
這不是問題,我打開一個會話渲染()。 – billyJoe
它完全*是*的問題。你呼叫登錄,然後......什麼都不是。您只需拖放到最後一行,再次呈現登錄表單。如果你想做其他事情,你需要實際上放一些代碼來做其他事情。 –