forms.py
class UserCreateProfileForm(forms.ModelForm):
phone_daytime = forms.IntegerField(required=False)
phone_mobile = forms.IntegerField(required=False)
class Meta:
model = UserProfile
fields = ['phone_daytime', 'phone_mobile']
def clean(self):
cleaned_data = self.cleaned_data
phone_daytime = cleaned_data.get('phone_daytime')
phone_mobile = cleaned_data.get('phone_mobile')
if not any([phone_daytime, phone_mobile]):
self._errors['phone_daytime'] = self.error_class(['Enter a valid phone number'])
del self.cleaned_data['phone_daytime']
self._errors['phone_mobile'] = self.error_class(['Enter a valid phone number'])
del self.cleaned_data['phone_mobile']
return cleaned_data
template.py
<tr>
<td>Daytime phone:</td><td>{{ form.phone_daytime }}{{ form.phone_daytime.errors }}</td>
</tr>
<tr>
<td>Mobile phone:</td><td>{{ form.phone_mobile }}{{ form.phone_mobile.errors }}</td>
</tr>
当我在“phone_daytime”字段中输入一些字符时,我收到此错误“Caught KeyError while rendering: 'phone_mobile'
”。来自所有其他验证的设备正在运行。
请参阅我的完整追溯
Environment:
Request Method: POST
Request URL: http://192.168.100.10/member/about-me/
Django Version: 1.3.7
Python Version: 2.7.0
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.humanize',
'django.contrib.staticfiles',
'south',
'collect',
'incident']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware')
Template error:
In template /root/Projects/ir/templates/incident/about_me.html, error at line 332
Caught KeyError while rendering: 'phone_mobile'
322 : <table width="100%">
323 : <tr>
324 : <td></td><td>* Mandatory Fields</td></tr>
325 : <tr>
326 : <td style="width:100px;">First name:</td><td>{{registerform.first_name|capfirst}}*{{registerform.first_name.errors}}</td>
327 : </tr>
328 : <tr>
329 : <td>Last name:</td><td>{{registerform.last_name}}*{{registerform.last_name.errors}}</td>
330 : </tr>
331 : <tr>
332 : <td>Daytime phone:</td><td>{{createprofile.phone_daytime|default:""}} {{createprofile.phone_daytime.errors }} </td>
333 : </tr>
334 : <tr>
335 : <td>Mobile phone:</td><td>{{createprofile.phone_mobile|safe}}{{createprofile.phone_mobile.errors }}</td>
336 : </tr>
337 : <tr>
338 : <td>Email:</td><td>{{registerform.email}}*{{registerform.email.errors }}</td>
339 : </tr>
340 : <tr>
341 : <td>User name</td><td>{{registerform.username}}*{{registerform.username.errors }}</td>
342 : </tr>
Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/root/Projects/ir/incident/decorators.py" in wrapper
17. return func(request, *args, **kwargs)
File "/root/Projects/ir/incident/views.py" in about_me
408. 'is_member_screen':is_member_screen
File "/usr/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
44. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/usr/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
188. return t.render(context_instance)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
123. return self._render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in _render
117. return self.nodelist.render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render
64. result = block.nodelist.render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node
73. result = node.render(context)
File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render
90. output = self.filter_expression.resolve(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in resolve
510. obj = self.var.resolve(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in resolve
653. value = self._resolve_lookup(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in _resolve_lookup
683. current = getattr(current, bit)
File "/usr/lib/python2.7/site-packages/django/forms/forms.py" in _errors
415. return self.form.errors.get(self.name, self.form.error_class())
File "/usr/lib/python2.7/site-packages/django/forms/forms.py" in _get_errors
112. self.full_clean()
File "/usr/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
268. self._clean_form()
File "/usr/lib/python2.7/site-packages/django/forms/forms.py" in _clean_form
296. self.cleaned_data = self.clean()
File "/root/Projects/ir/incident/forms.py" in clean
183. del self.cleaned_data['phone_mobile']
Exception Type: TemplateSyntaxError at /member/about-me/
Exception Value: Caught KeyError while rendering: 'phone_mobile'
实际上它应该通过错误消息,因为phone_daytime
和phone_mobile
都被声明为IntegerField
。但这会产生上述错误。需要帮助。
由于
答案 0 :(得分:1)
因为当你不在phone_mobile中输入任何内容时,它将不会存在于cleaning_data中,你试图从那里删除它,所以你会得到这个错误
答案 1 :(得分:1)
Deleting
以下行清除了上述错误,validation
也无问题
del self.cleaned_data['']
由于