ログイン フォームを修正できません。私が直面している問題は、ユーザーがメールまたはログインを入力せずにフォームを送信するたびに、このエラーが発生することです。この問題を解決するにはどうすればよいですか?
KeyError at /login/
emailRequest Method: POST
Request URL: http://127.0.0.1:8000/login/
Django Version: 1.4.3
Exception Type: KeyError
Exception Value: email
Traceback:
File "C:\Python26\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\o\17\mysite\pet\views.py" in LoginRequest
207. if request.POST and form.is_valid():
File "C:\Python26\lib\site-packages\django\forms\forms.py" in is_valid
124. return self.is_bound and not bool(self.errors)
File "C:\Python26\lib\site-packages\django\forms\forms.py" in _get_errors
115. self.full_clean()
File "C:\Python26\lib\site-packages\django\forms\forms.py" in full_clean
271. self._clean_form()
File "C:\Python26\lib\site-packages\django\forms\forms.py" in _clean_form
299. self.cleaned_data = self.clean()
File "C:\o\17\mysite\pet\forms.py" in clean
47. user = self.authenticate_via_email()
File "C:\o\17\mysite\pet\forms.py" in authenticate_via_email
62. email = self.cleaned_data['email']
フォーム.py
class LoginForm(forms.Form):
email = forms.CharField()
password = forms.CharField(
widget=forms.PasswordInput(render_value=False)
)
def clean(self):
user = self.authenticate_via_email()
if not user:
raise forms.ValidationError("Sorry, that login was invalid. Please try again.")
else:
self.user = user
return self.cleaned_data
def authenticate_user(self):
return authenticate(
username=self.user.username,
password=self.cleaned_data['password'])
def authenticate_via_email(self):
email = self.cleaned_data['email']
if email:
try:
user = User.objects.get(email__iexact=email)
if user.check_password(self.cleaned_data['password']):
return user
except ObjectDoesNotExist:
pass
return None
ビュー.py
def LoginRequest(request):
form = LoginForm(request.POST or None)
if request.method == 'POST' and form.is_valid():
user = form.authenticate_user()
login(request, user)
return HttpResponseRedirect(reverse('Hello'))
return render(request, 'login.html',{'form': form})
テンプレート
<form method="POST">
{% csrf_token %}
{{form.email}}{{form.email.errors}}
{{form.password}}{{form.password.errors}}
<input type = "submit" value = "Login" id="submit" />
</form>