用户注册案例(form写法)

用户注册案例(form写法)_第1张图片
image.png

一、切入点使用form

让页面生成写好的表单,从form切入,注册用户名,昵称,密码,确认密码,邮箱,验证码

  • 常用的函数

  • {{obj_register.non_field_errors.0}}显示all
  • from django.core.validators import RegexValidator用来写正则的
  • from django.core.exceptions import ValidationError用来捕获clean异常的
  • from django import forms 用来继承的
  • from django.forms import fields 用来调用字段的
  • from django.forms import widgets 用来引入页面插件的
  • 注意点

  • 验证码判断

首先我们将验证是储存在session中,而session是在request中,这里我们需要构造一个继承的函数,给我们form表单函数加入一个属性

class Base(object):
    def __init__(self,request,*args,**kwargs):
        self.request = request
        super(Base,self).__init__(*args,**kwargs)
  • clean_name 重写
  • 用户名、邮箱、判断是结合数据库查找判断是否存在
  • 验证码是去session判断
  • 多字段判断clean
  • 判断两次输入的密码是否正确使用clean
    当我们想给某个字段加上额外的判断时候,一定要重写clean_name
  • widgets.TextInput 为标签属性赋值
  • widgets.CheckboxInput({'value':"0"}),checkbox设置
from  django import forms
from  django.forms import fields
from  django.core.validators import RegexValidator
from .base import Base
from repository import models
from django.core.exceptions import  ValidationError
from django.forms import widgets
class accountform(Base,forms.Form):
    username = fields.CharField(
        label= "用名",
        widget= widgets.TextInput(attrs={'class':'form-control'}),
        validators=[RegexValidator(r'^[a-zA-Z0-9_-]{4,16}$','4到16位(字母,数字,下划线,减号)')],
        error_messages = {'required':'不能为空',}
    )

    nickname = fields.CharField(
        label="昵称",

        widget=widgets.TextInput(attrs={'class': 'form-control'}),
        validators=[RegexValidator(r'^[a-zA-Z0-9_-]{4,16}$', '4到16位(字母,数字,下划线,减号)')],
        error_messages={'required': '不能为空', }
    )
    password = fields.CharField(
        label="密码",
        widget=widgets.PasswordInput(attrs={'class': 'form-control'}),
        validators=[RegexValidator(r'^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$','以字母开头,字母,数字,减号,下划线')]
        ,error_messages = {'required': '不能为空', }
    )

    confirm_password = fields.CharField(
        label="密码",
        widget=widgets.PasswordInput(attrs={'class': 'form-control'}),
        validators=[RegexValidator(r'^[a-zA-Z]([a-zA-Z0-9]{5,19})+$', '以字母开头,字母,数字,减号,下划线')]
        ,error_messages = {'required':'不能为空',}
     )

    email = fields.EmailField(
        label="邮箱",
        widget=widgets.TextInput(attrs={'class': 'form-control'}),
        error_messages={'inisvalid':'请输入正确的邮箱','required':'邮箱不能为空'})

    check_code = fields.CharField(
        label="验码",
        error_messages={'required':'验证码不能为空'}
    )

    # avatar_img = fields.ImageField(
    #     label="头像",
    #     error_messages={'invalid_image':"请上传图片类型",'required':'不能为空'}
    # )

    def clean_username(self):
        v = self.cleaned_data['username']
        have_name = models.UserInfo.objects.filter(username=v).count()
        if have_name:
            raise ValidationError('用户名存在')
        return v

    def clean_email(self):
        v = self.cleaned_data['email']
        has_email = models.UserInfo.objects.filter(email=v).count()
        if has_email:
            raise ValidationError('邮箱存在')
        return v

    def clean_check_code(self):
        v = self.cleaned_data['check_code']
        if self.request.session.get('CheckCode').upper() != v.upper():
            raise ValidationError('验证码错误')
        return v
    def clean(self):
        value_dict = self.cleaned_data
        paw = value_dict.get('password')
        con_pax = value_dict.get('confirm_password')
        print(paw)
        if paw != con_pax:
            raise ValidationError('密码不一致')
        return  value_dict


你可能感兴趣的:(用户注册案例(form写法))