视图配置

  1. import re
  2. from django.shortcuts import render,redirect,HttpResponse
  3. from test01 import models
  4. from django import forms
  5. from django.views import View
  6. from django.core.exceptions import ValidationError
  7. from django.core.validators import RegexValidator
  8. def check_mobile_field(value):
  9. # 自定义的校验方法
  10. mobile_re = re.compile(r'0?(13|14|15|17|18|19)[0-9]{9}')
  11. if not mobile_re.match(value):
  12. raise ValidationError("手机号码格式错误")
  13. class form_view(forms.Form):
  14. name = forms.CharField(
  15. required=True, # 输入不能为空
  16. label="姓名:", # 页面上的input框名字
  17. initial="张三", # 框里的默认值
  18. help_text="输入姓名", # 帮助文本
  19. min_length=6,
  20. error_messages={"min_length":"长度最少6位","required":"输入值不能为空!"},
  21. widget=forms.widgets.TextInput()
  22. )
  23. password = forms.CharField(
  24. label="密码:",
  25. min_length=8,
  26. max_length=10,
  27. error_messages={"min_length": "长度最少8位","max_length":"长度最多10位" ,"required": "输入值不能为空!"},
  28. widget=forms.widgets.PasswordInput() # 秘文输入
  29. )
  30. r_password = forms.CharField(
  31. label="确认密码:",
  32. min_length=8,
  33. max_length=10,
  34. error_messages={"min_length": "长度最少8位","max_length":"长度最多10位" ,"required": "输入值不能为空!"},
  35. widget=forms.widgets.PasswordInput() # 秘文输入
  36. )
  37. mobile = forms.CharField(
  38. label="手机号码:",
  39. max_length=11,
  40. error_messages={"max_length":"长度最多11位" ,"required": "输入值不能为空!"},
  41. # validators=[check_mobile_field], # 调用自定义的正则方法
  42. validators=[RegexValidator('0?(13|14|15|17|18|19)[0-9]{9}',"手机号码格式错误")], # 正则校验
  43. widget=forms.widgets.TextInput()
  44. )
  45. # 局部钩子
  46. def clean_name(self):
  47. value = self.cleaned_data['name']
  48. if '中国' in value:
  49. raise ValidationError("含有敏感词汇: 中国")
  50. else:
  51. return value
  52. # 全局钩子
  53. def clean(self):
  54. value = self.cleaned_data
  55. p1 = value['password']
  56. p2 = value['r_password']
  57. if p1 != p2:
  58. self.add_error('r_password','两次输入不一致') # 给某个字段添加报错信息
  59. self.add_error('password','两次输入不一致')
  60. raise ValidationError("error") # 没用但是得写 raise
  61. class Loginview(View):
  62. def get(self,request):
  63. from_objs = form_view()
  64. return render(request,'login.html',{"from_objs":from_objs})
  65. def post(self,request):
  66. from_objs = form_view(request.POST)
  67. # print(from_objs.fields) # {'name': <django.forms.fields.CharField object at 0x102c6fb80>, 'password': <django.forms.fields.CharField object at 0x102c81df0>}
  68. # print(from_objs.is_valid()) # bool
  69. if from_objs.is_valid():
  70. print(from_objs.cleaned_data) # {'name': '张三', 'password': '11111111'}
  71. else:
  72. return render(request,'login.html',{"from_objs":from_objs})
  73. return HttpResponse('ok')

html配置

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Login</title>
  6. </head>
  7. <body>
  8. <form action="/login/" method="post" novalidate> {# novalidate 不使用页面校验 #}
  9. {% csrf_token %}
  10. <div>
  11. <label> {{ from_objs.name.label }} </label> <!-- 获取标签 在页面上现实的是中文姓名 -->
  12. {{ from_objs.name }} <!-- input标签 -->
  13. {{ from_objs.name.help_text }} <!-- 提示 -->
  14. <span> <font color="red"> {{ from_objs.name.errors.0 }} </font> </span>
  15. </div>
  16. <div>
  17. <label> {{ from_objs.password.label }} </label>
  18. {{ from_objs.password }}
  19. <span> <font color="red"> {{ from_objs.password.errors.0 }} </font> </span>
  20. </div>
  21. <div>
  22. <label> {{ from_objs.r_password.label }} </label>
  23. {{ from_objs.r_password }}
  24. <span> <font color="red"> {{ from_objs.r_password.errors.0 }} </font> </span>
  25. </div>
  26. <div>
  27. <label> {{ from_objs.mobile.label }} </label>
  28. {{ from_objs.mobile }}
  29. <span> <font color="red"> {{ from_objs.mobile.errors.0 }} </font> </span>
  30. </div>
  31. <button>提交</button>
  32. </form>
  33. </body>
  34. </html>

image.png