1

フォーム.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」として追加すると、1 桁目の後に「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

ありがとう

4

5 に答える 5

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
于 2015-02-12T18:29:34.090 に答える