我正在尝试创建一个自定义登录表单。以下是我到目前为止所拥有的。我的问题是,无论我做什么,登录都不会进行身份验证。即使我有正确的电子邮件和密码,我总是会得到:
请输入正确的用户名和密码。两个地方都要注意大小写。
我认为这是因为我正在检查电子邮件和密码。
*这是我的表格。*
class EmailAuthenticationForm(AuthenticationForm):
username = forms.EmailField(label=_('Email Address'))
redirect_url = forms.CharField(
widget=forms.HiddenInput, required=False)
def __init__(self, host=None, *args, **kwargs):
self.host = host
super(EmailAuthenticationForm, self).__init__(*args, **kwargs)
这是我的看法。**
class AccountAuthView(FormView):
template_name = 'accounts/login.html'
form_class = EmailAuthenticationForm
redirect_field_name = 'next'
def get_form_class(self):
return self.form_class
def form_valid(self, form):
auth_login(self.request, form.get_user())
return HttpResponseRedirect(form.cleaned_data['redirect_url'])
def get(self, request, *args, **kwargs):
if request.user.is_authenticated():
return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
return super(AccountAuthView, self).get(
request, *args, **kwargs)