zoukankan      html  css  js  c++  java
  • django_Form_自定义钩子

    from django import forms
    from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
    class RegForm(forms.Form):
    
        username=forms.CharField(min_length=5,
                                 max_length=12,
                                 widget=forms.TextInput(attrs={"class":"form-control","placeholder":"username"}))
    
    
        password=forms.CharField(widget=forms.PasswordInput(attrs={"class":"form-control","placeholder":"password"}))
    
        repeat_password=forms.CharField(widget=forms.PasswordInput(attrs={"class":"form-control","placeholder":"password"}))
    
        email=forms.EmailField(widget=forms.EmailInput(attrs={"class":"form-control","placeholder":"email"}))
    
        valid_code=forms.CharField(widget=forms.TextInput(attrs={"class":"form-control","placeholder":"valid_code"}))
    
    
        def __init__(self,request,*args,**kwargs):
            super(RegForm,self).__init__(*args,**kwargs)
            self.request=request
    
    #红色部分为自定义的钩子函数 :
    
        # def clean_password(self):  #  关于密码字段的钩子
        #
        #     if len(self.cleaned_data["password"])>8:
        #         return self.cleaned_data["password"] # sadfsad234532
        #     else:
        #         raise ValidationError("密码小于8位!")
        #
        # def clean_username(self):
        #
        #     if self.cleaned_data["username"].isdigit() or self.cleaned_data["username"].isalpha():
        #         raise ValidationError("用户名必须包含数字与字母!")
        #     else:
        #         return self.cleaned_data["username"]
        #
        #
        #
        # def clean_valid_code(self):
        #     if self.cleaned_data["valid_code"].upper() ==self.request.session["valid_code"].upper():
        #         return self.cleaned_data["valid_code"]
        #
        #
        #     else:
        #         raise ValidationError("验证码错误!")
    
    
    
    
    
        # def clean(self):
        #     if self.cleaned_data["password"]==self.cleaned_data["repeat_password"]:
        #         return self.cleaned_data
        #
        #     else:
        #         raise ValidationError("密码不一致")


  • 相关阅读:
    给右键 添加dos命令
    js模拟系统无刷新跳回登录页1
    MBProgressHUD.h file not found
    建立个人博客网站
    <转>提高iOS开发效率的方法和工具
    设置模块功能设计思路及代码实现
    经验之谈
    'NSInteger' (aka 'long') to 'int32
    OCR技术
    升级Xcode6.4插件失效解决办法
  • 原文地址:https://www.cnblogs.com/575dsj/p/7909802.html
Copyright © 2011-2022 走看看