forms.py
class PhoneForm(forms.ModelForm):
number1 = forms.IntegerField(required=False,error_messages={'invalid':'Enter a valid phone number'})
number2 = forms.IntegerField(required=False,error_messages={'invalid':'Enter a valid phone number'})
number3 = forms.IntegerField(required=False,error_messages={'invalid':'Enter a valid phone number'})
class Meta:
model = PhoneInfo
fields = ['name1','number1','name2','number2','name3','number3','emergency','emergency_number']
models.py
class PhoneInfo(models.Model):
user = models.ForeignKey(User, null=True)
name1 = models.CharField('Name', max_length=100, null=True, blank=True)
number1 = models.CharField('Number',max_length=20, null=True, blank=True)
name2 = models.CharField('Name', max_length=100, null=True, blank=True)
number2 = models.CharField('Number', max_length=20, null=True, blank=True)
name3 = models.CharField('Name', max_length=100, null=True, blank=True)
number3 = models.CharField('Number',max_length=20, null=True, blank=True)
emergency = models.CharField('Emergency', max_length=100, null=True, blank=True)
emergency_number = models.CharField('Emergency Number',max_length=20, null=True, blank=True)
nmber1,number2和number3用于保存11位电话号码。我的问题是如果我在保存'0'之后将第1位添加为'0'从表格字段中消失。我在之后添加“0”保存和显示的第一个数字。问题是:08854215452保存后第一个零消失。我想在保存带有STD代码的电话号码的同时显示。
Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
101. request.path_info)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
252. sub_match = pattern.resolve(new_path)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
252. sub_match = pattern.resolve(new_path)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
158. return ResolverMatch(self.callback, args, kwargs, self.name)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py" in _get_callback
164. self._callback = get_callable(self._callback_str)
File "/usr/lib/python2.7/site-packages/django/utils/functional.py" in wrapper
124. result = func(*args)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py" in get_callable
91. lookup_view = getattr(import_module(mod_name), func_name)
File "/usr/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/root/Projects/ir_uploaded_copy/ir/setting/views.py" in <module>
22. from setting.forms import *
File "/root/Projects/ir_uploaded_copy/ir/setting/forms.py" in <module>
151. class PhoneForm(forms.ModelForm):
File "/root/Projects/ir_uploaded_copy/ir/setting/forms.py" in PhoneForm
162. emergency_number = forms.RegexField(regex=r'^\+?(\d{3}-?\d+{7})$', error_messages = {'invalid':'Enter a valid phone number'})
File "/usr/lib/python2.7/site-packages/django/forms/fields.py" in __init__
437. regex = re.compile(regex)
File "/usr/lib/python2.7/re.py" in compile
190. return _compile(pattern, flags)
File "/usr/lib/python2.7/re.py" in _compile
245. raise error, v # invalid expression
Exception Type: error at /setting/call/
Exception Value: multiple repeat
由于
答案 0 :(得分:2)
永远不要将电话号码保存为IntegerFields
。您所看到的行为对于任何编程语言中的整数都是正常的。
>> myInt = 00001
>> print myInt
>> 1
你应该把它存储为什么?您要么a)编写自己的字段,b)选择CharField
或选择RegexField
说到phonenumbers我通常更喜欢正则表达式字段 如果 你在世界的一个固定区域,瑞典就会有以下正则表达式:< / p>
phone_number = forms.RegexField(regex=r'^\+?(\d{3}-?\d+{7})$', error_messages = {'invalid_phonenumber': _("Not a valid Swedish phone number.")}
答案 1 :(得分:2)
永远不要将电话号码视为整数!始终使用CharFields,如有必要,可以进行一些验证。确保您不仅允许数字,还包括“ - ”,“”等。
您的ModelForm可能如下所示:
class PhoneForm(forms.ModelForm):
class Meta:
model = PhoneInfo
fields = ['name1','number1','name2','number2','name3','number3','emergency','emergency_number']
def clean_number1(self):
n = self.cleaned_data.get('number1')
for allowednondigit in '- ./':
n.replace(allowednondigit, '')
for char in n:
if char not in '0123456789':
raise forms.ValidationError("Please only use digits, spaces, dots, slash and dash characters")
return n
这样,您只会在数据库中存储数字(如果这是您真正想要的数字),同时仍然让您的用户按照他习惯的方式编写电话号码。
答案 2 :(得分:0)
由于这个原因,我只看到models.CharField
中存储的电话号码。
不要在表单上使用forms.IntegerField
,而是使用forms.CharField
。
答案 3 :(得分:0)
是 - 整数将忽略前导零。您可以将0设为隐式 (实际上它是隐式的)或将其转换为文本字段。
答案 4 :(得分:0)
我发布了基于mawimawi's answer的工作解决方案。
class PhoneForm(forms.ModelForm):
class Meta:
model = PhoneInfo
fields = ['name1','number1','name2','number2','name3','number3','emergency','emergency_number']
def clean_number1(self):
n = self.cleaned_data.get('number1')
allowed_characters = '0123456789- ./'
for char in n:
if char not in allowed_characters:
raise forms.ValidationError("Please only use digits, spaces, dots, slash and dash characters")
return n