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("密码不一致")


  • 相关阅读:
    hdu 5532 Almost Sorted Array(模拟)
    hdu 2612 Find a way(bfs)
    hdu 2660 Accepted Necklace(dfs)
    reactjs学习一(环境搭配react+es6+webpack热部署)
    途牛banner自动轮播
    web app开发技巧总结
    20个为前端开发者准备的文档和指南
    Github上最受关注的前端大牛 快来膜拜把!
    怎么才能成为优秀的前端开发工程师?
    Web前端知识技能大汇总
  • 原文地址:https://www.cnblogs.com/575dsj/p/7909802.html
Copyright © 2011-2022 走看看