views.py
  1. from django.shortcuts import render,HttpResponse
  2. from django import forms
  3. import json
  4. import re
  5. from django.core.exceptions importValidationError
  6. # Create your views here.
  7. def mobile_validate(value):
  8. mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
  9. ifnot mobile_re.match(value):
  10. raiseValidationError('手机号码格式错误')
  11. classInputForm(forms.Form):
  12. # 下面使用的变量名必须和html中input标签的name值相同
  13. # forms 的字段类型包括:IntegerField,CharField,URLField,EmailField,DateField等,但是没有手机号
  14. # required=True表示对输入做验证
  15. # error_messages 自定制提示信息
  16. username = forms.CharField(required=True, error_messages={'required':'用户名不能为空'})
  17. password = forms.CharField(required=True,
  18. min_length=8,
  19. max_length=20,
  20. error_messages={'required':'密码不能为空','min_length':'至少6位',
  21. 'max_length':'至多10位'}
  22. )
  23. num = forms.IntegerField(error_messages={'required':'不能为空','invalid':'必须是数字'})
  24. # 自定制验证方法关键就是参数validators,和自己的函数关联起来
  25. phone = forms.CharField(validators=[mobile_validate,],)
  26. # django给标签加sytle,关键参数widget ,forms.TextInput 表示生成type="text"的input标签,改成Textarea则生成<textarea>标签
  27. test = forms.CharField(widget=forms.TextInput(attrs={'class':'in_tmp'}))
  28. choice =(
  29. (1,'北京'),
  30. (2,'上海')
  31. )
  32. t_choice = forms.IntegerField(widget=forms.Select(choices=choice))
  33. def login(request):
  34. if request.POST:
  35. objPost =InputForm(request.POST)
  36. ret = objPost.is_valid()
  37. if ret:
  38. print(objPost.clean())
  39. # else:
  40. # # from django.forms.utils import ErrorDict
  41. # print(type(objPost.errors),objPost.errors.as_json())
  42. return render(request,'login.html',{'data': objPost})
  43. else:
  44. objGet =InputForm()
  45. return render(request,'login.html',{'data': objGet})
login.html
  1. <!DOCTYPE html>
  2. <htmllang="en">
  3. <head>
  4. <metacharset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .error-msg{
  8. color:red;
  9. }
  10. .in_tmp{
  11. border:1px solid #6a34ff ;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <formaction="/login/"method="POST">
  17. <div>
  18. <div>
  19. {{ data.username }}
  20. {% if data.errors.username %}
  21. <spanclass="error-msg">{{ data.errors.username.0 }}</span>
  22. {% endif %}
  23. </div>
  24. <div>
  25. {{ data.password }}
  26. {% if data.errors.password %}
  27. <spanclass="error-msg">{{ data.errors.password.0 }}</span>
  28. {% endif %}
  29. </div>
  30. <div>
  31. {{ data.num }}
  32. {% if data.errors.num %}
  33. <spanclass="error-msg">{{ data.errors.num.0 }}</span>
  34. {% endif %}
  35. </div>
  36. <div>
  37. {{ data.phone }}
  38. {% if data.errors.phone %}
  39. <spanclass="error-msg">{{ data.errors.phone.0 }}</span>
  40. {% endif %}
  41. </div>
  42. <div>
  43. {{ data.test }}
  44. {% if data.errors.test %}
  45. <spanclass="error-msg">{{ data.errors.test.0 }}</span>
  46. {% endif %}
  47. </div>
  48. <div>
  49. {{ data.t_choice }}
  50. {% if data.errors.t_choice %}
  51. <spanclass="error-msg">{{ data.errors.t_choice.0 }}</span>
  52. {% endif %}
  53. </div>
  54. <inputtype="submit"value="提交"/>
  55. </div>
  56. </form>
  57. </body>
  58. </html>
效果图:
 
select 标签显示数据库数据: