我知道dojango的stackoverflow没有多少,但我想我还是会问的。你知道吗
Dojango对RegexField的描述如下:
class RegexField(DojoFieldMixin, fields.RegexField):
widget = widgets.ValidationTextInput
js_regex = None # we additionally have to define a custom javascript regexp, because the python one is not compatible to javascript
def __init__(self, js_regex=None, *args, **kwargs):
self.js_regex = js_regex
super(RegexField, self).__init__(*args, **kwargs)
我在我的生活中也是这样用的表单.py地址:
post_code = RegexField(js_regex = '[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}')
# &
post_code = RegexField(attrs={'js_regex': '[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}'})
不幸的是,这两者都给了我:
TypeError: __init__() takes at least 2 arguments (1 given)
如果我使用以下选项:
post_code = RegexField(regex = '[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}')
我得到以下HTML:
<input name="post_code" required="true" promptMessage="" type="text" id="id_post_code" dojoType="dijit.form.ValidationTextBox" />
有人能告诉我我做错了什么吗?你知道吗
该错误与
super().__init__
调用有关。如果fields.RegexField
是标准的DjangoRegexField
,那么它需要regex
关键字参数,如文档所示。因为你没有通过它,你得到TypeError
。如果它应该与js_regex
相同,那么在超级调用中传递它。你知道吗三天后,我发现您需要发送
regex
和js_regex
,尽管regex
没有使用:[哦,是的!您还需要将小部件声明为
ValidationTextInput
]相关问题 更多 >
编程相关推荐