当您在模板中呈现表单时,Django将运行def clean
。现在假设def clean
引发了一个异常。当DEBUG = True
时,仅吞下了Exception。这意味着我在本地看不到异常。但是当DEBUG = False
投入生产时,我的页面突然间崩溃了!显然,这完全否定了本地开发环境的观点。
有人知道如何使Django总是引发它在表单的纯净方法中遇到的异常吗?
views.py
class MyForm(forms.Form):
foo = forms.CharField(max_length=100)
def clean(self, *args, **kwargs):
print('This is how I know when clean runs')
raise Exception('Wah!')
def my_view(request):
context = {'my_form': MyForm({'foo': 'bar'})}
return render(request, 'my_template.html', context=context)
my_template.html
When settings.DEBUG == True, this will render just fine.
But when settings.DEBUG == False, the page crashes.
{{ my_form }}