我试图分离视图(控制器)的身份验证。在我看来,代码似乎并不在一个好地方。然后我得到了下面代码中显示的结果。这是错误的,我可以提高更多,还是担心这是愚蠢的?
视图.py
def sign_in(request):
form = SignInForm(request.POST or None)
if form.is_valid():
login(request, form.user)
return redirect('user/home/')
context = {'sign_in_form' : form}
context.update(csrf(request))
return render_to_response('app_user/sign_in.html', context)
表格.py
class SignInForm(forms.Form):
username = forms.CharField(required=True, max_length=30, label='Username')
password = forms.CharField(widget=forms.PasswordInput, max_length=30, label='Password')
user = None
def clean(self):
data = self.cleaned_data
if super(SignInForm, self).is_valid():
user = authenticate(username=data.get('username'),password=data.get('password'))
if user is None:
raise forms.ValidationError("Wrong user or password")
if not user.is_active:
raise forms.ValidationError("User is disabled")
self.user = user
return data