开发者

Django (dojango) FieldRegex not being passed

I know there is not much on stackoverflow on dojango, but I thought I'd ask anyway.

Dojango describes RegexField as follows:

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)

And I am using it as so in my 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}'})

Unfortunately these both give me:

TypeError: __init__() takes at least 2 arguments (1 given)

If I use the following:

post_code = RegexField(regex = '[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}')

I get the following HTML开发者_运维百科:

<input name="post_code" required="true" promptMessage="" type="text" id="id_post_code" dojoType="dijit.form.ValidationTextBox" />

Can anyone tell me what I might be doing wrong?


After three days of beavering away I fould that you need to send regex and js_regex, though regex is not used:

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}'
        }
    )
)

[Oh yeah! and you also need to declare the widget as a ValidationTextInput]


The error is related to super().__init__ call. If fields.RegexField is standard Django RegexField, then it requires regex keyword argument, as documented. Since you don't pass it, you get TypeError. If it's supposed to be the same as js_regex, then pass it along in the super call.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜