python pycharm验证最小值和最大值

问题描述 投票:0回答:1

我想将标题限制在3到25个字符之间。我无法让MinLengthValidator工作。我是Pycharm和python的新手

class Video(models.Model):
    title = models.CharField(max_length=255)
    url = models.URLField()
    youtube_id = models.CharField(max_length=255)

    def clean_title_length(self):
        data = self.cleaned_data['title']
        length= len(data)
        if length < 3 or length > 25:
            raise ValidationError("The length of the title must be 
                 between 3 and 25 characters")
        else:
            return data

views.py

 def add_video(request):
     form = VideoForm()
     if request.method == 'POST':
          filled_form = VideoForm(request.POST)
          if filled_form.is_valid():

以上表格始终返回有效。

有关如何使此范围验证器工作的建议?

TIA

python pycharm
1个回答
0
投票

数据是“标题”,因此您的长度始终为5

© www.soinside.com 2019 - 2024. All rights reserved.