django的form 登录组件
Posted zhangqing979797
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django的form 登录组件相关的知识,希望对你有一定的参考价值。
1.
了解form 组件的原理
1.建立好form组件 class>>>
2.了解需要先is_valid() 判断是否可以取值
成功 form.cleaned_data 查看成功认证的字典
失败 form.errors 查看失败的键值对
3.自定义 钩子
4.在def clean 比较,通过
form.errors.get("__all__"):
获取哪里的
raise ValidationError("两次密码不一致")
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" 7 integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 8 </head> 9 <body> 10 11 {#<form action="" method="post">#} 12 {# {% csrf_token %}#} 13 {# <p>用户名 <input type="text" name="user" ><span class="error">{{ error.user.0 }}</span></p>#} 14 {# <p>密码 <input type="password" name="pwd"> <span class="error">{{ error.pwd.0 }}</span></p>#} 15 {# <p>邮箱 <input type="text" name="email"> <span class="error">{{ error.email.0 }}</span></p>#} 16 {# <input type="submit">#} 17 {# #} 18 {#</form>#} 19 <h3>reg第一种</h3> 20 {#<form action="" method="post">#} 21 {# #} 22 {# {% csrf_token %}#} 23 {# {{ form.as_p }}#} 24 {# <input type="submit">#} 25 {#</form>#} 26 27 <h3>reg第二种即使写错了也会保留输入的信息</h3> 28 {#<div class="container">#} 29 {# <div class="row">#} 30 {# <div class="col-md-8 col-md-offset-2">#} 31 {# <form action="" method="post" novalidate>#} 32 {# {% csrf_token %}#} 33 {# <p> 用户名 {{ form.user }}<span class="error">{{ errors.user.0 }}</span> </p>#} 34 {# <p>密码 {{ form.pwd }}<span class="error">{{ errors.pwd.0 }}</span></p>#} 35 {# <p>邮箱 {{ form.email }}<span class="error">{{ errors.email.0 }}</span></p>#} 36 {##} 37 {# <input type="submit" class="btn btn-success pull-right">#} 38 {# </form>#} 39 {# </div>#} 40 {# </div>#} 41 {#</div>#} 42 {##} 43 44 <h3>reg第三种 完美版</h3> 45 <div class="container"> 46 <div class="row"> 47 <div class="col-md-8 col-md-offset-2"> 48 <form action="" method="post" novalidate> 49 {% csrf_token %} 50 {% for foo in form %} 51 52 <div class="from-group"> 53 <label for="">{{ foo.label }}</label> 54 {# 这里的foo的属性为 form表单的一系类标签#} 55 {# foo.errors.0 指的是当出现错误信息返回时,错误信息是以表单的字段位键,值是错误信息,以取得 #} 56 {{ foo }} <span class="error">{{ foo.errors.0 }}</span> 57 {# 当返回的是自定义错误时,需要在def clean拿变区分是否两次密码一样,取到的在__all__里面#} 58 {% if foo.label == "确认密码" %} 59 <span class="errors">{{ g_error|default:"" }}</span> 60 61 {% endif %} 62 63 </div> 64 {% endfor %} 65 <input type="reset"> 66 <input type="submit" class="btn btn-success pull-right"> 67 68 </form> 69 </div> 70 </div> 71 </div> 72 73 74 </body> 75 </html>
1 from django.shortcuts import render, HttpResponse 2 3 # Create your views here. 4 # 1.引入自定义条件和form 5 from django.core.exceptions import NON_FIELD_ERRORS, ValidationError 6 from django import forms 7 from django.forms import widgets 8 9 10 class UserFrom(forms.Form): 11 msg = {"required": "该字段不能为空"} 12 user = forms.CharField(min_length=5, label="用户名", error_messages=msg, 13 widget=widgets.TextInput(attrs={"class": "form-control"})) 14 pwd = forms.CharField(error_messages=msg, label="密码", widget=widgets.TextInput(attrs={"class": "form-control"})) 15 16 r_pwd = forms.CharField(error_messages=msg, 17 min_length=5, 18 label="确认密码", 19 widget=widgets.PasswordInput(attrs={"class": "form-control"})) 20 email = forms.EmailField(error_messages={"invalid": "邮箱格式错误"}, 21 label="邮箱", 22 widget=widgets.EmailInput(attrs={"class": "form-control"})) 23 24 def clean_user(self): 25 val = self.cleaned_data.get("user") # 从cleaned_data取到user 26 ret = UserInfo.objects.filter(user=val).first() # 在数据库列表中找到user的字典 27 if not ret: # 不存在,返回user,再讲user传递给数据库进行录入 28 return val 29 else:# 这里讲错误的信息传递给error 30 raise ValidationError("用户名已经处在!") 31 32 def clean_pwd(self): 33 val = self.cleaned_data.get("pwd") 34 if val.isdigit(): 35 return ValidationError("密码不能是纯数字") # 这里讲错误的信息传递给error 36 else: 37 return val 38 39 def clean(self):#这里源码是 返回self.cleaned_data,需要在这里进行比对,若果错误的话返回一个错误的信息返回 40 pwd = self.cleaned_data.get("pwd") 41 r_pwd = self.cleaned_data.get("r_pwd") 42 43 if pwd and r_pwd: 44 if pwd == r_pwd:#这里要清除错误的键值对,因为auth_user 没有r_password 45 del self.cleaned_data["r_pwd"] 46 47 return self.cleaned_data 48 else: 49 raise ValidationError("两次密码不一致") 50 else: 51 return self.cleaned_data 52 53 54 from app01.models import UserInfo 55 56 57 def reg(request): 58 if request.method == "POST": 59 print("request.POST", request.POST) # 打印post请求 60 61 form = UserFrom(request.POST) 62 print("form", form) 63 if form.is_valid(): 64 print("form.cleaned_data.user", form.cleaned_data) # 打印通过is_valid一串字典,以form字段为键的字典 65 UserInfo.objects.create(**form.cleaned_data) 66 return HttpResponse("OK") 67 else: 68 errors = form.errors 69 print("form.errors", form.errors) # 接收到以form为字段的键,含错误信息的值 70 print("form.cleaned_data", form.cleaned_data) # 打印录入的字典 71 # print(form.errors.get("user")[0])#从error中精确获取值 72 print("------>", form.errors.get("__all__"))#将在def clean方法的错误信息返回 73 74 if form.errors.get("__all__"): # 获取全局的变量 75 g_error = form.errors.get("__all__")[0] # 将错误返回 76 return render(request, "reg.html", locals()) 77 78 else: 79 form = UserFrom() 80 return render(request, "reg.html", locals())
以上是关于django的form 登录组件的主要内容,如果未能解决你的问题,请参考以下文章
Django Form组件实例:登录界面[Form表单提交,Ajax提交]
CSIC_716_20200113Django入门---forms组件session和cookie登录认证装饰器