我正在使用 re 库通过正则表达式验证电话号码,并且我之前在其他项目上使用过相同的确切代码,它运行良好,但现在我得到了
“NoneType”对象没有“匹配”属性
模型.py
from django.core.exceptions import ValidationError
import re
def validate_phone(value): #the validation regex here to be used in models later
if re.match('^(010|011|012)[0-9]{8}',value) is None :
raise ValidationError(u'%s is not a valid number' % value)
if UserProfile.objects.filter(phone_number=value).count() > 1:
raise ValidationError("Phone number already exists")
class UserProfile(User):
#.... some other attributes
mobile_number = models.CharField(validators=[validate_phone],null=True, blank=True, unique=True,max_length=16)
当我尝试从管理员更改手机号码进行测试时,我收到此错误:
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
366. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
196. return view(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "/Library/Python/2.7/site-packages/django/db/transaction.py" in inner
209. return func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in change_view
1035. if form.is_valid():
File "/Library/Python/2.7/site-packages/django/forms/forms.py" in is_valid
124. return self.is_bound and not bool(self.errors)
File "/Library/Python/2.7/site-packages/django/forms/forms.py" in _get_errors
115. self.full_clean()
File "/Library/Python/2.7/site-packages/django/forms/forms.py" in full_clean
272. self._post_clean()
File "/Library/Python/2.7/site-packages/django/forms/models.py" in _post_clean
326. self.instance.clean_fields(exclude=exclude)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in clean_fields
844. setattr(self, f.attname, f.clean(raw_value, self))
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in clean
201. self.run_validators(value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in run_validators
154. v(value)
File "/Users/fadykamal/Work/Projects/Django/ShoghlanahProject/shoghlanah/models.py" in validate_phone
23. if re.match('^(010|011|012)[0-9]{8}',value) is None :
Exception Type: AttributeError at /admin/shoghlanah/userprofile/2/
Exception Value: 'NoneType' object has no attribute 'match'