gpt4 book ai didi

python - 为什么我的 Django 表单没有引发验证错误?

转载 作者:太空狗 更新时间:2023-10-30 02:42:12 24 4
gpt4 key购买 nike

以下是我的表单代码:

class ConfirmEmailForm(forms.Form):
email = forms.EmailField()
subscribe = forms.IntegerField()
code = forms.CharField(max_length=80)

def clean_subscribe(self):
value = bool(self.cleaned_data['subscribe'])
self.cleaned_data['subscribe'] = value
return value

def clean(self):
cleaned_data = super(ConfirmEmailForm, self).clean()
email = cleaned_data['email']
code = cleaned_data['code']
user = User.objects.filter(username=email).first()
if not user:
raise forms.ValidationError('Email not found')
self.cleaned_data['user'] = user
if user.emailverification.is_key_expired():
raise forms.ValidationError('Link expired, please regenerate')
if not user.emailverification.key == code:
raise forms.ValidationError('Invalid Link')
return cleaned_data

我将此表单用于 GET 请求。现在,当我在没有 email 字段的情况下发送请求时,我原以为会在 cleaned_data 处引发错误,但是它没有引发任何错误,而是我得到了 KeyError 在下一行。

显然它会引发 KeyError 因为它不存在。但我的问题是,为什么没有在 super(ConfirmEmailForm, self).clean() 本身引发错误。

这是完整的回溯:

Environment:


Request Method: GET
Request URL: http://localhost:8000/user/confirm/?code=g&subscribe=1

Django Version: 1.9.6
Python Version: 3.5.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'nightreads',
'nightreads.posts',
'nightreads.user_manager']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/Users/avi/.virtualenvs/nightreads/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)

File "/Users/avi/.virtualenvs/nightreads/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/avi/.virtualenvs/nightreads/lib/python3.5/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)

File "/Users/avi/.virtualenvs/nightreads/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)

File "/Users/avi/Documents/code/nightreads/nightreads/user_manager/views.py" in get
54. if form.is_valid():

File "/Users/avi/.virtualenvs/nightreads/lib/python3.5/site-packages/django/forms/forms.py" in is_valid
161. return self.is_bound and not self.errors

File "/Users/avi/.virtualenvs/nightreads/lib/python3.5/site-packages/django/forms/forms.py" in errors
153. self.full_clean()

File "/Users/avi/.virtualenvs/nightreads/lib/python3.5/site-packages/django/forms/forms.py" in full_clean
363. self._clean_form()

File "/Users/avi/.virtualenvs/nightreads/lib/python3.5/site-packages/django/forms/forms.py" in _clean_form
390. cleaned_data = self.clean()

File "/Users/avi/Documents/code/nightreads/nightreads/user_manager/forms.py" in clean
30. email = cleaned_data['email']

Exception Type: KeyError at /user/confirm/
Exception Value: 'email'

为什么会这样?我该如何解决这个问题?在 clean 中访问经过验证和清理的数据的正确方法是什么?

最佳答案

如果 clean 方法遇到错误,它将把它添加到错误列表中,但不会引发错误。

您可以找到代码行 here .

        except ValidationError as e:
self.add_error(name, e)

您可以使用 cleaned_data.get('email') 解决它,如果未找到该值,它将返回 None - 然后您可以对其进行检查。

关于python - 为什么我的 Django 表单没有引发验证错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37327684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com