zoukankan      html  css  js  c++  java
  • django第三天课上总结

    day16
    ###1.内容回顾
    - 1.路由
        - 1.静态路由(url里是固定的值)
            - user_list   
        - 2.动态路由(url里使用正则表达式)
            - ^ $ . * + ? d w [0-9]
        - 3.分组和命名分组
            - ()    捕获参数  -->  字符串
                - 捕获参数 按照位置传参
            - (?P<name>xxx)
                - 捕获参数 按照关键字传参
        - 4.include
            - 路由分发
        - 5.URL的命名和反向解析
            - name URL别名  
            - 静态路由
                 - url(r'^user_list/',user_list ,name='user_list'),
                 
            - 反向解析
                - 模板
                    - {% url 'user_list' %}   --> '/app01/user_list/'
                    
                - PY文件
                    - from django.conf.urls import reverse
                    - reverse('user_list')    --> '/app01/user_list/'
                    
            - 分组
                - url(r'^user_list/(d+)/',user_list ,name='user_list'),
                
            - 反向解析
                - 模板
                    - {% url 'user_list' 1 %}   --> '/app01/user_list/1/'
                    
                - PY文件
                    - from django.conf.urls import reverse
                    - reverse('user_list',args=(1,))    --> '/app01/user_list/1/'
                    
            - 命名分组
                - url(r'^user_list/(?P<pk>d+)/',user_list ,name='user_list'),
                
            - 反向解析
                - 模板
                    - {% url 'user_list' 1 %}      --> '/app01/user_list/1/'
                    - {% url 'user_list' pk=1 %}   --> '/app01/user_list/1/'
                    
                - PY文件
                    - from django.conf.urls import reverse
                    - reverse('user_list',args=(1,))    --> '/app01/user_list/1/'
                    - reverse('user_list',kwargs={'pk':1})    --> '/app01/user_list/1/'
        - 6.namespace
            - url(r'^app01/', include('app01.urls', namespace='app01')),        
            - {% url 'app01:user_list' 1 %}  
            
    - 2.视图
        - mvc mtv 的区别    
        - FBV基于函数的视图 CBV基于类的视图
        - 定义CBV
        ```python
         from django.views import View
        
        class AddPub(View):
            
            def get(self,request,*args,**kwargs):
                # 处理GET请求
                
            def post(self,request,*args,**kwargs):
                # 处理post请求
        ```
       
        - 使用
        ```python
        url(r'^add_pub/',AddPub.as_view()),
        ```        
            
        - as_view()
            - view函数中要去执行dispatch()
            
            - dispatch -->  通过反射获取对应请求方式对应的方法 
            
        - request
            - request.method  请求方式 GET POST
            - request.GET     url上参数  查询条件
            - request.POST    POST请求提交的数据
            - request.body    请求体 
            - request.FILES   上传的文件   enctype="multipart/form-data"
            - request.META    请求头的信息 HTTP_ 
            - request.path_info  路径  不包含IP 端口 参数
            - request.COOKIES
            - request.session
            
            - request.get_full_path()   路径  不包含IP 端口 包含参数
            - request.is_ajax()
            
        - response
            - HttpRespnse('字符串')   -->  返回字符串
            - render(request,'模板的文件名',{})    --> 返回完整的页面
            - redirect('地址')   --> 重定向   Location: 地址
            - JsonResponse({})   --> content-type: applicaton/json   传非字典的数据 safe=False        
        
    - 3.模板
        - {{}}   {%  %}
        
        - 变量
            - render(request,'模板的文件名',{k:v})
            - {{ k }} 
            - .key
            - .属性或者方法
            - .索引
        
        - 过滤器filter
            - {{ k|filter }}   {{ k|filter:参数 }}
            - default:'xx'
            - add
            - safe
            - date  日期时间格式化 date:'Y-m-d H:i:s'
            - length
            - truncatechars 
            - truncatewords 
        
        - {%  %}
        
        - if 不支持算数运算  不支持连续判断 10>5>1  
        - {% if 条件 %}
        
        - {% elif 条件 %}
        
        - {% else %}
        
        - {% endif %}
        
        - for 
        - {% for i in list  %}
            - {{ forloop }}    counter counter0  revcounter  revcounter0 first last  parentloop:{}
        - {% endfor %}
        
        - with  
        - csrf_token  name=csrfmiddlewaretoken 
        
        - 母版和继承
            - 母版
                - 1.把多个页面的公共部分提取出来 -->  HTML   base.html 
                - 2.定义block 
                
            - 继承:
                - 1.第一行写上{% extends 'base.html' %}
                - 2.复写block块
                
        - 静态文件:
            - {% load static %}
            
            - {% static '静态文件的相对路径' %}  STATIC_URL = '/static/'
            - {% get_static_prefix %}  -->  STATIC_URL = '/static/'
            
    - 4.外键
        - all_books = Book.objects.all()
        
        - book_obj.title 
        - book_obj.pub       -->  所关联的对象
        - book_obj.pub_id    -->  所关联的对象id
            
        - 新增
        ```python
        Book.objects.create(titl='xxx',pub=对象)
        Book.objects.create(titl='xxx',pub_id=对象id)
        ```
        
        - 修改
        ```python
        book_obj.title= 'xxx'
        # book_obj.pub=对象
        book_obj.pub_id=对象id
        book_obj.save()
        ```
            
        - 删除
        ```python
        book_obj.delete()
        ```
        
    ###2.今日内容
    - 1.ORM操作
        - https://www.cnblogs.com/maple-shaw/articles/9323320.html
        - https://www.cnblogs.com/maple-shaw/articles/9403501.html
        - 1.常用字段和参数
            - 常用字段
                - AutoField
                - CharField
                - IntegerField
                - DateTimeField
                - BooleanField
                - DecimalField
                - FileField
            - 参数
            - null    数据库可以为空
            - blank   数据校验时可以为空
            - default      默认值
            - unique     唯一约束
            - verbose_name   中文提示
            - choices    可选择的数据
            
            ```python
            class Meta:
                # 数据库中生成的表名称 默认 app名称 + 下划线 + 类名
                db_table = "person"
    
                # admin中显示的表名称
                verbose_name = '个人信息'
    
                # verbose_name加s
                verbose_name_plural = '所有用户信息'
    
                # 联合索引
                index_together = [
                    ("pub_date", "deadline"),  # 应为两个存在的字段
                ]
    
                # # 联合唯一索引
                unique_together = (("name", "age"),)  # 应为两个存在的字段
            ```
             
        - 2.必知必会13条
            - models.Person.objects.all()    
        - 3.单表的双下划线
        - 4.外键的查询
        - 5.多对多的操作
    - 2.ORM进阶
        - 1.聚合和分组
        - 2.F、Q、事务
            - 事务
            '
                begin;  开启事务;
                .....  
                commit; 提交 
            '
    
    - 3.cookie和session
        - https://www.cnblogs.com/maple-shaw/articles/9502602.html
    
        - cookie
            - 保存在浏览器上的一组组键值对,由服务器让浏览器进行设置的,下次自动携带相应的cookie去访问服务器.
        
        - Django中的操作:
            - 设置:    Set-Cookie: is_login=1; Path=/
                - ret= redirect('/index/')
                - ret.set_cookie('is_login','1')
                
                - ret.set_signed_cookie(key,value,salt='加密盐',...)
                
            - 获取:  Cookie
                - request.COOKIES  {}
                - request.COOKIES.get(key)
                - request.get_signed_cookie(key,default='',salt='加密盐',...)
                
            - 删除:   Set-Cookie ''   max_age=0
                - ret= redirect('/index/')
                - ret.delete_cookie('is_login')
        
        - session
            - 保存在服务器上的一组组键值对,必须依赖cookie
            
        - Django中的操作:
            - 设置:
                - request.session['is_login'] = '1'
            - 获取:
                - request.session['is_login']
                - request.session.get(is_login)
                
            - 删除:
                - request.session.pop('is_login')
                - request.session.delete()   # 删除整个session数据  不删除cookie   sessionid
                - request.session.flush()    # 删除整个session数据  删除cookie   sessionid
                
            - 其他
            # 将所有Session失效日期小于当前日期的数据删除
            - request.session.clear_expired()        
            - request.session.set_expiry(5)   设置超时时间
  • 相关阅读:
    18、led驱动程序的实现
    17、字符设备控制技术
    16、驱动访问大揭秘
    14、字符驱动编程模型
    个人开发—进度记录(一)
    个人开发—需求
    学习进度条
    敏捷开发第二阶段个人(七)
    敏捷开发第二阶段个人(六)
    敏捷开发第二阶段个人(五)
  • 原文地址:https://www.cnblogs.com/lilyxiaoyy/p/11298297.html
Copyright © 2011-2022 走看看