Django(dojango)FieldRegex未通过

2024-04-19 15:31:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道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" />

有人能告诉我我做错了什么吗?你知道吗


Tags: toselfnoneinitjsargscodejavascript
2条回答

该错误与super().__init__调用有关。如果fields.RegexField是标准的Django RegexField,那么它需要regex关键字参数,如文档所示。因为你没有通过它,你得到TypeError。如果它应该与js_regex相同,那么在超级调用中传递它。你知道吗

def __init__(self, js_regex, *args, **kwargs):
    self.js_regex = js_regex
    super(RegexField, self).__init__(regex, *args, **kwargs)

三天后,我发现您需要发送regexjs_regex,尽管regex没有使用:

post_code = RegexField(
    regex='',
    required = True,
    widget=ValidationTextInput(
        attrs={
            'invalid': 'Post Code in incorrect format',
            'regExp': '[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}'
        }
    )
)

[哦,是的!您还需要将小部件声明为ValidationTextInput]

相关问题 更多 >