day18 分页+form验证+中间件
参考课件:
http://www.cnblogs.com/wupeiqi/articles/6144178.html
http://www.cnblogs.com/wupeiqi/articles/5246483.html
1 s16Day18 2 3 上节回顾: 4 1. Model操作 5 - v = models.UserInfo.objects.all() 6 - v = models.UserInfo.objects.values("xx") 7 - v = models.UserInfo.objects.values_list() 8 9 - v = models.UserInfo.objects.filter(...) 10 11 - 双下滑线进行跨表 12 FK: 13 正向操作(含FK): 14 15 反向操作(表名_set): 16 17 删除时,操作:关联删除?关联设置默认值?... 待续 18 19 M2M: 20 待续 21 22 23 性能相关:待续 24 select_related('ut') 25 26 2. session和cookies 27 28 29 3. Ajax 30 31 $.ajax({ 32 url: '', 33 type: 'POST', 34 data: {user:'xxxx'}, 35 dataType: 'JSON', 36 success:function(data){ 37 # data-> 字符串 38 # data = JSON.parse(data) 39 } 40 }) 41 42 今日内容: 43 1. 分页 44 45 2. Form验证 46 功能: 47 - 验证用户请求 48 - 自动生成HTML(保留上次提交的内容) 49 详细: 50 class Foo(forms.Form): 51 usename = fields.CharField() # 正则,插件 52 字段: 53 CharField 字符串 54 EmailField 字符串(邮箱格式) 55 IntegerField 字符串(数字格式) 56 GenericIPAddressField 字符串(IP格式) 57 FileField 文件对象 58 RegexField 字符串(自定义) 59 ChoiceField 多选 60 61 插件: 62 widgets.TextInput 63 widgets.PasswordInput 64 widgets.Textarea 65 66 widgets.Select(attrs={'class': 'form-control'},choices=[(1,'普通用户'),(2,"超级用户")]) 67 widgets.SelectMultiple(attrs={'class': 'form-control'},choices=[(1,'普通用户'),(2,"超级用户")]) 68 69 widget=widgets.RadioSelect(choices=((1,'上海'),(2,'北京'),)) 70 71 widget=widgets.CheckboxInput() 72 widget=widgets.CheckboxSelectMultiple() 73 74 widget=widgets.FileInput() 75 76 单表: 77 78 FK表: 79 问题:数据源无法实时更新 80 重写构造函数: 81 def __init__(self,*args,**kwargs): 82 super(UserForm,self).__init__(*args,**kwargs) 83 self.fields['ut_id'].widget.choices = models.UserType.objects.values_list('id','name') 84 85 3. Model操作补充 86 87 4. 中间件 88 process_request(self,request) 89 process_view(self, request, callback, callback_args, callback_kwargs) 90 process_template_response(self,request,response) 91 process_exception(self, request, exception) 92 process_response(self, request, response) 93 94 MIDDLEWARE = [ 95 'django.middleware.security.SecurityMiddleware', 96 'django.contrib.sessions.middleware.SessionMiddleware', 97 'django.middleware.common.CommonMiddleware', 98 'django.middleware.csrf.CsrfViewMiddleware', 99 'django.contrib.auth.middleware.AuthenticationMiddleware', 100 'django.contrib.messages.middleware.MessageMiddleware', 101 'django.middleware.clickjacking.XFrameOptionsMiddleware', 102 "md.middleware.M1", 103 "md.middleware.M2", 104 ] 105 5. 缓存 106 107 6. 信号 108 109 110 作业: 111 112 1. 主机管理: 113 Form表单进行验证 114 分页 115 ** Ajax提交:Form表单验证 ** 116 117 2. 抽屉 118 设计数据库表结构