如何使用 modelform 和 multiwidget

发布于 2024-10-19 20:19:32 字数 2312 浏览 1 评论 0原文

这一切都是新手!我正在努力在前端显示显示为 (xxx)xxx-xxxx 的电话字段。下面是我的代码。我的问题是 1. 所有字段都是强制性的,由于某种原因,手机的行为不符合预期。即使将其留空,也不会抱怨 2.我如何测试此小部件的功能

class USPhoneNumberWidget(forms.MultiWidget):
        def __init__(self,attrs=None):
            widgets = (forms.TextInput(attrs={'size':'3','maxlength':'3'}),forms.TextInput(attrs={'size':'3','maxlength':'3'}),forms.TextInput(attrs={'size':'3','maxlength':'4'}))
            super(USPhoneNumberWidget,self).__init__(widgets,attrs=attrs)

        def decompress(self, value):
            if value:
                val = value.split('-')
                return [val[0],val[1],val[2]]
            return [None,None,None]

        def compress(self, data_list):
            if data_list[0] and data_list[1] and data_list[2]:
                ph1 = self.check_value(data_list[0])
                ph2 = self.check_value(data_list[1])
                ph3 = self.check_value(data_list[2])
                return '%s''%s''%s' %(ph1,ph2,ph3)
            else:
                return None

        def check_value(self,val):
            try:
                if val.isdigit():
                    return val
            except:
                raise forms.ValidationError('This Field has to be a number!')

        def clean(self, value):
            try:
                value = re.sub('(\(|\)|\s+)','',smart_unicode(value))
                m = phone_digits_re.search(value)
                if m:
                    return u'%s%s%s' % (m.group(1),m.group(2),m.group(3))
            except:
                raise ValidationError('Phone Number is required.')

        def value_from_datadict(self,data,files,name):
            val_list = [widget.value_from_datadict(data,files,name+'_%s' %i) for i,widget in enumerate(self.widgets)]
            try:
                return val_list
            except ValueError:
                return ''

        def format_output(self,rendered_widgets):
            return '('+rendered_widgets[0]+')'+rendered_widgets[1]+'-'+rendered_widgets[2]

    class CustomerForm(ModelForm):
    phone = forms.CharField(required=True,widget=USPhoneNumberWidget())
       class Meta:
            model = Customer
            fields = ('fname','lname','address1','address2','city','state','zipcode','phone')

在模型中,空白和 null 不是 true。 任何意见都非常感谢。谢谢

Newbie to all this! i'm working on displaying phone field displayed as (xxx)xxx-xxxx on front end.below is my code. My question is 1. all fields are mandatory, for some reason,phone is not behaving as expected.Even if it is left blank its not complaining and 2.how can i test this widget's functionality

class USPhoneNumberWidget(forms.MultiWidget):
        def __init__(self,attrs=None):
            widgets = (forms.TextInput(attrs={'size':'3','maxlength':'3'}),forms.TextInput(attrs={'size':'3','maxlength':'3'}),forms.TextInput(attrs={'size':'3','maxlength':'4'}))
            super(USPhoneNumberWidget,self).__init__(widgets,attrs=attrs)

        def decompress(self, value):
            if value:
                val = value.split('-')
                return [val[0],val[1],val[2]]
            return [None,None,None]

        def compress(self, data_list):
            if data_list[0] and data_list[1] and data_list[2]:
                ph1 = self.check_value(data_list[0])
                ph2 = self.check_value(data_list[1])
                ph3 = self.check_value(data_list[2])
                return '%s''%s''%s' %(ph1,ph2,ph3)
            else:
                return None

        def check_value(self,val):
            try:
                if val.isdigit():
                    return val
            except:
                raise forms.ValidationError('This Field has to be a number!')

        def clean(self, value):
            try:
                value = re.sub('(\(|\)|\s+)','',smart_unicode(value))
                m = phone_digits_re.search(value)
                if m:
                    return u'%s%s%s' % (m.group(1),m.group(2),m.group(3))
            except:
                raise ValidationError('Phone Number is required.')

        def value_from_datadict(self,data,files,name):
            val_list = [widget.value_from_datadict(data,files,name+'_%s' %i) for i,widget in enumerate(self.widgets)]
            try:
                return val_list
            except ValueError:
                return ''

        def format_output(self,rendered_widgets):
            return '('+rendered_widgets[0]+')'+rendered_widgets[1]+'-'+rendered_widgets[2]

    class CustomerForm(ModelForm):
    phone = forms.CharField(required=True,widget=USPhoneNumberWidget())
       class Meta:
            model = Customer
            fields = ('fname','lname','address1','address2','city','state','zipcode','phone')

In models blank and null are not true.
Any input it highly appreciated.Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2024-10-26 20:19:32

这是电话字段:

phone = forms.CharField(label = 'Phone',widget=USPhoneNumberWidget() 
class USPhoneNumberWidget(forms.MultiWidget):
        """
        A widget that splits phone number into areacode/next3/last4 with textinput.
        """
        def __init__(self,attrs=None):
        widgets = (forms.TextInput(attrs={'size':'3','maxlength':'3'}),forms.TextInput(attrs={'size':'3','maxlength':'3'}),forms.TextInput(attrs={'size':'4','maxlength':'4'}))
        super(USPhoneNumberWidget,self).__init__(widgets,attrs=attrs)

    def decompress(self, value):
        if value:
            val = value
            return val[:3],val[3:6],val[6:]
        return None,None,None

    def compress(self, data_list):
        if data_list[0] and data_list[1] and data_list[2]:
            return '%s''%s''%s' %(data_list[0],data_list[1],data_list[2])
        else:
            return None

    def value_from_datadict(self,data,files,name):
        val_list = [widget.value_from_datadict(data,files,name+'_%s' %i) for i,widget in enumerate(self.widgets)]
        if val_list:
            return '%s''%s''%s' %(val_list[0],val_list[1],val_list[2])

    def format_output(self,rendered_widgets):
        return '( '+rendered_widgets[0]+' )'+rendered_widgets[1]+' - '+rendered_widgets[2]

但根据您在数据库中存储电话#的方式,“返回”行将被更改。在这里,我接受它为 (xxx)-xxx-xxxx 格式。在压缩中,它按顺序接收 ph_0(areacode)、ph_1(next 3)、ph_2(last4)。但我将其存储为 xxxxxxxxxx。

Firebug 帮助我更好地理解返回值应该是什么。当我知道如何进行测试时,我会更新答案。

Here is the phone field:

phone = forms.CharField(label = 'Phone',widget=USPhoneNumberWidget() 
class USPhoneNumberWidget(forms.MultiWidget):
        """
        A widget that splits phone number into areacode/next3/last4 with textinput.
        """
        def __init__(self,attrs=None):
        widgets = (forms.TextInput(attrs={'size':'3','maxlength':'3'}),forms.TextInput(attrs={'size':'3','maxlength':'3'}),forms.TextInput(attrs={'size':'4','maxlength':'4'}))
        super(USPhoneNumberWidget,self).__init__(widgets,attrs=attrs)

    def decompress(self, value):
        if value:
            val = value
            return val[:3],val[3:6],val[6:]
        return None,None,None

    def compress(self, data_list):
        if data_list[0] and data_list[1] and data_list[2]:
            return '%s''%s''%s' %(data_list[0],data_list[1],data_list[2])
        else:
            return None

    def value_from_datadict(self,data,files,name):
        val_list = [widget.value_from_datadict(data,files,name+'_%s' %i) for i,widget in enumerate(self.widgets)]
        if val_list:
            return '%s''%s''%s' %(val_list[0],val_list[1],val_list[2])

    def format_output(self,rendered_widgets):
        return '( '+rendered_widgets[0]+' )'+rendered_widgets[1]+' - '+rendered_widgets[2]

But depending on how you store the phone# in db 'return' line is to be changed. here I'm accepting it as (xxx)-xxx-xxxx format.In compress it receives ph_0(areacode),ph_1(next 3),ph_2(last4) in that order.but I'm storing it as xxxxxxxxxx.

Firebug helped me understand better about what return values should be. I'll update the answer when i come to know how testing could be done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文