DJango表单与自定义__init__不验证 [英] DJango form with custom __init__ not validating
本文介绍了DJango表单与自定义__init__不验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用这个代码自定义窗体的窗口小部件时,它不会被验证。如果我注释掉 def __init __(..)
它可以正常工作。
When I use this code to customize my form's widget, it will not validate. If I comment out def __init__(..)
it works fine.
class CommentForm(forms.Form):
def __init__(self, *args, **kwargs):
self.wysiwyg = kwargs.pop('wysiwyg', False)
super(CommentForm, self).__init__()
if self.wysiwyg:
self.fields['comment_text'].widget = SummernoteWidget()
else:
self.fields['comment_text'].widget = forms.Textarea(attrs={'rows':2})
comment_text = forms.CharField()
我已经能够解决这个问题,工作表单(无 init )与无效表单之间的区别是这样的:
I've been able to troubleshoot this far, and the difference between the working form (no init) and the invalid form, is this:
不正确的格式与 init :
CommentForm bound=False, valid=Unknown, fields=(comment_text)
形式:
CommentForm bound=True, valid=Unknown, fields=(comment_text)
是否有问题,我该如何解决?
Is bound the problem and how do I fix it?
谢谢!
推荐答案
尝试这个..可能会工作
Try this .. might work
class CommentForm(forms.Form):
def __init__(self, *args, **kwargs):
try:
self.wysiwyg = kwargs['wysiwyg']
except KeyError:
self.wysiwyg = None
super(CommentForm, self).__init__(*args, **kwargs)
if self.wysiwyg:
self.fields['comment_text'].widget = SummernoteWidget()
else:
self.fields['comment_text'].widget = forms.Textarea(attrs={'rows':2})
comment_text = forms.CharField()
这篇关于DJango表单与自定义__init__不验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文