NameError:未定义名称"RegexValidator"

use*_*207 3 python django django-models python-2.7

这是我的models.py中的代码.

from django.db import models

class Person(models.Model):
     contactNumber = models.IntegerField(max_length = 11, unique = True, validators = [[RegexValidator(regex='^\d{11}$', message='Length has to be 11 numbers including 0', code='Invalid number')]])
     picture = models.ImageField(upload_to = 'folder_name', default = 'folder_name/default-image.jpg')
     jobTitle = models.CharField(max_length = 30)
Run Code Online (Sandbox Code Playgroud)

当我运行时python manage.py sql individual,我收到contactNumber字段的错误.

NameError: name 'RegexValidator' is not defined

和图片字段的此错误.

django.core.exceptions.ImproperlyConfigured: Neither Pillow nor PIL could be imported: No module named Image

我是Python和Django的新手.我应该怎么做才能纠正这两个字段?

Dan*_*man 11

这是基本的Python,为了使用你需要的任何东西来定义它或在当前模型中导入它.在您的情况下,您需要from django.core.validators import RegexValidator在模型文件的顶部进行操作.

对于第二个,错误消息告诉您需要知道的全部内容:您需要在系统中安装Pillow或(不太优选)PIL.ImageField的文档提到了这一点并具有相关链接.


Dr.*_*lch 6

您可能需要导入

 from django.core.validators import RegexValidator
Run Code Online (Sandbox Code Playgroud)

您还需要先安装枕头,然后才能使用它