我创建了一个呈现模板的视图,并在提交表单时更新了两个模型。
我目前只验证一个表单(is_valid()
),但另一个表单也正在清理,即使我没有调用它们上的is_valid()
或'clean()
方法
我的观点:
def update_two_models_at_once(request):
if request.method == "POST":
form1 = Form1(request.POST)
form2 = Form2(request.POST)
if form1.is_valid():
#form1.save()
#post = form2.save(commit=False)
#post.save()
return HttpResponseRedirect(reverse('stock:forms'))
else:
form2 = Form2()
return render(request, 'update_models.html', {'form2': form2})
我在Form2
中有一个打印声明,每次提交表单时都会执行该声明,即使我只是尝试验证Form1
。
有什么我做错了或似乎丢失了吗?追溯回到这一行:
return render(request, 'update_models.html', {'form2': form2})
当我第一次呈现页面时,它显示正常。但是一旦我提交了页面,Form1
就不会验证(我故意让它不验证),当它再次呈现模板时,它会调用Form2
的clean方法。这有什么用?我现在只想验证并清除Form1
。
编辑:
我的Form2代码:
class Form2(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def clean(self):
cleaned_data = super().clean()
serial = cleaned_data.get('serial')
print(error)
class Meta:
model = Form2Model
fields = '__all__'
提交模板时,在print(error)
到达时追溯:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Python36\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python36\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "...\views.py", line 166, in update_two_models_at_once
return render(request, 'update_models.html', {'form': form})
File "C:\Python36\lib\site-packages\django\shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Python36\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Python36\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Python36\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "C:\Python36\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Python36\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Python36\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Python36\lib\site-packages\django\template\loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "C:\Python36\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Python36\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Python36\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Python36\lib\site-packages\django\template\loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "C:\Python36\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Python36\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Python36\lib\site-packages\django\template\defaulttags.py", line 307, in render
match = condition.eval(context)
File "C:\Python36\lib\site-packages\django\template\defaulttags.py", line 880, in eval
return self.value.resolve(context, ignore_failures=True)
File "C:\Python36\lib\site-packages\django\template\base.py", line 676, in resolve
obj = self.var.resolve(context)
File "C:\Python36\lib\site-packages\django\template\base.py", line 802, in resolve
value = self._resolve_lookup(context)
File "C:\Python36\lib\site-packages\django\template\base.py", line 843, in _resolve_lookup
current = getattr(current, bit)
File "C:\Python36\lib\site-packages\django\forms\forms.py", line 174, in errors
self.full_clean()
File "C:\Python36\lib\site-packages\django\forms\forms.py", line 377, in full_clean
self._clean_form()
File "C:\Python36\lib\site-packages\django\forms\forms.py", line 404, in _clean_form
cleaned_data = self.clean()
File "...\forms.py", line 80, in clean
print(error)
NameError: name 'error' is not defined