博客园项目使用Form组件和Ajax实现注册需要注意的点
一.注册相关的知识点
首先是自己新建一个应用,users,然后在里面定义一个文件forms.py,然后在里面定义需要校验的字段和相应的钩子函数,关于forms组件,笔者在前一篇博客中详细地进行了说明,不清楚的同学可以去翻阅。forms.py如下:
# _*_ coding:utf-8 _*_ import re from django import forms from django.core.validators import RegexValidator from django.forms import widgets, ValidationError from .models import UserProfile class RegisterForm(forms.Form): username = forms.CharField( required=True, min_length=6, max_length=15, error_messages={ "required": "用户名不能为空", "min_length": "用户名长度不能小于6", "max_length": "用户名长度不能大于15" }, widget=widgets.TextInput(attrs={"placeholder": "请输入您的用户名", "class": "form-control"}) ) password = forms.CharField( required=True, min_length=6, max_length=30, error_messages={ "required": "密码不能为空", "min_length": "密码长度不能小于6", "max_length": "密码长度不能超过30", }, widget=widgets.PasswordInput(attrs={"placeholder": "请输入密码", "class": "form-control"}) ) password_again = forms.CharField( required=True, min_length=6, max_length=30, error_messages={ "required": "密码不能为空", "min_length": "密码长度不能小于6", "max_length": "密码长度不能超过30", }, widget=widgets.PasswordInput(attrs={"placeholder": "请再次输入您的密码", "class": "form-control"}) ) email = forms.EmailField( required=True, error_messages={ "required": "邮箱不能为空", "invalid": "邮箱格式错误", }, widget=widgets.EmailInput(attrs={"placeholder": "请输入您的邮箱", "class": "form-control"}) ) mobile = forms.CharField( required=True, max_length=11, min_length=11, error_messages={ "required": "手机号码不能为空", "min_length": "手机号码长度必须是11位", "max_length": "手机号码长度必须是11位", }, validators=[RegexValidator("\d+", "手机号码只能是数字")], widget=widgets.TextInput(attrs={"placeholder": "请您输入你的手机号码", "class": "form-control"}) ) def clean_username(self): name = self.cleaned_data.get("username") is_exist = UserProfile.objects.filter(username=name).exists() if is_exist: raise ValidationError("抱歉,用户名已经存在") return name def clean_password(self): """ 再加入一个判断,不能是纯数字或者纯字母 """ password = self.cleaned_data.get("password") result = re.findall("(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$", password) if result: return password else: raise ValidationError("密码必须包含大小写,长度至少为8") def clean(self): if self.cleaned_data.get("password") == self.cleaned_data.get("password_again"): return self.cleaned_data else: raise ValidationError("对不起,两次输入的密码不一致")
http://www.jb51.net/article/43303.htm
http://www.365mini.com/page/jquery-prop.htm
https://www.cnblogs.com/Showshare/p/different-between-attr-and-prop.html
http://blog.csdn.net/tang7837010/article/details/18260049
https://www.cnblogs.com/songdongdong/p/6340373.html
http://www.css88.com/jqapi-1.9/prop/
http://www.cnblogs.com/haiyan123/p/7875440.html