zoukankan      html  css  js  c++  java
  • day17 Django学习三

    参考博客:
    http://www.cnblogs.com/wupeiqi/articles/5237704.html

    http://www.cnblogs.com/wupeiqi/articles/5246483.html

      1 s16day17
      2 
      3 
      4 上节内容回顾:
      5     
      6     路由系统:
      7         1.   /index/                ->  def index(request)
      8         2.   
      9             /detail-(d+)/          -> def detail(request,nid)
     10             /detail-(?P<nid>d+)/   -> def detail(request,nid)
     11             
     12         3. 
     13             /index/                ->  def index(request)    name=n1
     14             使用别名生成URL:
     15                 模板语言: {% url n1 %}          -> /index/ 
     16                 视图函数:
     17                             reverse(name="n1")   -> /index/ 
     18                             
     19         4. 
     20         
     21             /web/   include("app01.urls")
     22     视图函数:
     23         1. 函数至少一个参数
     24         2. request
     25             request.method
     26             request.GET
     27             request.POST (请求头:Content-Type:application/x-www-form-urlencoded; charset=UTF-8)
     28             request.body
     29             request.FILES
     30             ...
     31         3. 
     32             return HttpResponse(..)
     33             return render()
     34             return redirect()
     35             
     36     模板引擎:
     37         
     38         1. 基本语法
     39         
     40             return renderI(request, 'xxx.html', {'v': [1,2,3,4],'d':{'k1':'v1','k2':'v2'} })
     41             
     42             xxx.html
     43                 {{v.2}}
     44                 
     45                 {% for i in d %}
     46                     {{i}} --> key
     47                 {% endfor %}
     48                 
     49                 
     50                 {% for k,v in d.items %}
     51                     {{k}}--{{v}}
     52                 {% endfor %}
     53     
     54         2. 函数
     55         
     56             Django提供函数
     57             simple_tag
     58             filter
     59             
     60      ORM操作:
     61         1. 创建表
     62             
     63             class UserInfo(models.Model):
     64                 # nid = models.AutoField(primary_key=True)   int
     65                 # nid = models.BigAutoField(primary_key=True)  long
     66                 name = models.CharField(max_length=32)
     67                 pwd = models.CharField(max_length=32)
     68             
     69             
     70             
     71         
     72         2. 操作表
     73      
     74             q = models.UserInfo.objects.all()
     75             Queryset = [obj(id,name,pwd),obj(id,name,pwd),obj(id,name,pwd),]
     76      
     77      
     78             q = models.UserInfo.objects.values('name','pwd')
     79             Queryset = [{"name":'alex','pwd':123},{"name":'alex1','pwd':123sfd},{"name":'alex1','pwd':123sfd},]
     80      
     81      
     82             q = models.UserInfo.objects.values_list('name','pwd')
     83             Queryset = [('alex',123),('alex',123),('alex',123),('alex',123),]
     84      
     85      
     86      
     87             q = models.UserInfo.objects.filter(name='alex')
     88             [obj,]
     89             
     90             
     91             q = models.UserInfo.objects.get(name='alex')
     92             q = models.UserInfo.objects.filter(name='alex').first()
     93             
     94             
     95             ...
     96             
     97             
     98             
     99             
    100 今日内容:
    101 
    102     1. FBV和CBV
    103         
    104         FBV:
    105            
    106             url(r'^index/', views.index),
    107            
    108             def index(request):
    109                 print('.....')
    110                 if request.method == 'GET':
    111                     pass
    112                 elif request.method == 'POST':
    113                     pass
    114                 return HttpResponse('....')
    115            
    116         
    117         CBV:
    118             url(r'^user/', views.User.as_view()),
    119             
    120             
    121             class User(View):
    122                 def dispatch(self, request, *args, **kwargs):
    123                     print('before')
    124                     obj = super(User,self).dispatch(request, *args, **kwargs)
    125                     print('after')
    126                     return obj
    127 
    128                 def get(self,request):
    129                     print("get...")
    130                     return HttpResponse('...')
    131 
    132                 def post(self,request):
    133                     print("post...")
    134                     return HttpResponse('...')
    135     
    136     2. ORM操作
    137            
    138            a. 创建表
    139                 一对多
    140                 多对多
    141                     - 创建第三表:
    142                         - 自己定义第三张表   :列无限制
    143                         - ManyToManyField字段: 列限制(三)
    144                     - 无法直接,只能通过ManyToManyField字段进行间接操作
    145                 
    146            b. 操作
    147                 正向
    148                     dp
    149                 反向
    150                     userinfo
    151                     userinfo_set
    152                     
    153     3. Cookie
    154         在浏览器上保存的简直对
    155         应用:
    156             可做用户登录
    157             做投票
    158     4. session    
    159         服务器端保存的简直对
    160         {
    161             asdfasdfasdf: {'user':'asdf','pws':'asdf'}
    162         
    163         }
    164 
    165     
    166     5. Ajax操作
    167         # 
    168         
    169         $.ajax({
    170             url: '/aj/',                # 提交地址
    171             type: "POST",               # 提交方式
    172             data: {uuu: u, ppp:p},      # 提交数据
    173             dataType: "JSON",
    174             success:function (data) {   # 回调函数,登录成功后自动执行
    175                 # 将字典形式的字符串,发序列化成为字典对象(json对象)
    176                 # var data_dict = JSON.parse(data);
    177                 
    178                 if(data_dict.status){
    179                     location.href = "/home/"
    180                 }else{
    181                     alert(data_dict.error);
    182                 }
    183             }
    184         })
    课堂笔记
     1    作业: 主机管理
     2     
     3         1. 主机表(FK),业务线表
     4         2. 用户表(M2M),   用户主机关系表
     5         3. 功能:
     6             a. Ajax登录
     7             b.  
     8                 主机表(主机信息+业务线信息)
     9             c. ***
    10                 当前用户管理的所有主机表(主机信息+业务线信息)
    11                 
    12             d. 业务线管理(增、修改、删除)
    13                 - 对话框
    14                 - 新URL
    作业
  • 相关阅读:
    405
    406
    4-1
    3-11
    3-10
    3-9
    3-8
    3-7
    3-5
    3-4
  • 原文地址:https://www.cnblogs.com/liyongsan/p/6885540.html
Copyright © 2011-2022 走看看