tôi hiện đang thử nghiệm django 1.5 và nó mô hình người dùng tùy chỉnh, nhưng tôi đã một số vấn đề hiểu biết tôi đã tạo ra một lớp người dùng trong ứng dụng tài khoản của tôi, trông giống như:Authenticate với 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'
tôi có thể đăng ký một cách cẩn thận người dùng, người được lưu trữ trong bảng account_user của tôi. Bây giờ, tôi có thể đăng nhập bằng cách nào? Tôi đã thử với
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})
nhưng người dùng là Không những nó render biểu mẫu đăng nhập :( Tại sao autheticate tôi trả tôi Không? Bất kỳ ý tưởng?
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
bạn đang mong đợi gì xảy ra sau khi bạn gọi 'đăng nhập() 'với người dùng mới? –
nó không phải là câu hỏi, một render() tôi đoán với một phiên mở ra. – billyJoe
Nó hoàn toàn * là * câu hỏi. Bạn gọi đăng nhập, sau đó ... không có gì. Bạn chỉ cần thả xuống dòng cuối cùng, nơi nó làm cho biểu mẫu đăng nhập lại. Nếu bạn muốn làm một cái gì đó khác, bạn sẽ cần phải thực sự đặt một số mã vào để làm một cái gì đó khác. –