Django(dojango)FieldRegex没有通过

时间:2011-08-25 19:33:37

标签: python django dojo dijit.form

我知道在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)

我在我的forms.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" />

谁能告诉我自己可能做错了什么?

2 个答案:

答案 0 :(得分:1)

经过三天的离职后,我发现您需要发送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]

答案 1 :(得分:0)

该错误与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)