zoukankan      html  css  js  c++  java
  • Python之Django总结

    一.Django

    总结django知识点

     

    一、视图函数:

        请求对象-----------request:

              1、HttpRequest.body:         请求原数据

              2、HttpRequest.path:            一个字符串,表示请求的路径组件(不含域名)

              3、HttpRequest.method  

              4、HttpRequest.GET

              5、HttpRequest.POST

              6、HttpRequest.FILES

              7、HttpResquest.user:        一个AUTH_USER_MODEL 类型的对象

       响应对象:

             return   HttpResponse("")       返回字符串实例

             return   render(request,"template",{"":""})     返回字符串实例

             return   rredirect("/index/")      重定向

    二、  模板语言

           首先在views视图函数里写函数,返回一个渲染页面:

          views:

                  def foo (request): 

                       name="li"

                       l=[111,2222,3333]

                      d={"info":[obj1,obj2...]}

                      return  render(request,"index.html",locals())

         1、变量:{{name}}

                  -------深度查询  句点符.     如:{{d.info.0.name}}

                  -------过滤器{{name|date:"Y-m-d"}}

        2.标签:

          for循环

              {%for i in l%}

                 {{i}}

              {%empty%}

               <p>没有符合的条件</p>

              {%endfor%}       

         if判断语句

              {%if name="li"%}

                 <p> Yes</p>

              {%elif%}

              {%endif....%}

       3、模板语法

             (1)在母版中base.html:  {%block con%}{%endblock%}

             (2)继承母版 index.html:

                                                    {%extends "base.html"%}         继承

                                                    {%block con%}

                                                    {%endblock%}

    三、ORM之models模型

        1、 映射关系:

                   sql中表名-----------Python的类名

                   sql中的字段---------Python的类属性

                   sql中的记录---------Python的类对象

     连表操作(基于对象查询):

    一对多的查询:
                                
                                    实例1:查询主键为4的文章的分类名称(正向查询,按字段)
                                           
                                           article_obj=models.Article.objects.get(nid=4)
                                           print(article_obj.category.title)
                                    
                                           SELECT * FROM "blog_article" WHERE "blog_article"."nid" = 4;  // category_id=2
                                           SELECT * FROM "blog_category" WHERE "blog_category"."nid" = 2; 
    
                                    实例2:查询用户yuan发表过的所有的文章(反向查询,表名_set)
                                           yuan=models.UserInfo.objects.get(username="yuan")
                                           book_list=yuan.article_set.all()  # QuerySet 
                                           
                                       
                                 
                                多对多的查询:
                                    
                                     实例3:查询主键为4的文章的标签名称(正向查询,按字段)     
                                         article_obj=models.Article.objects.get(nid=4)                                
                                         tag_list=article_obj.tags.all()   #  是通过Article2Tag找到tag表中关联的tag记录
                                         for i in tag_list:
                                              print(i.title)
                                          
                                     实例4:查询web开发的这个标签对应的所有的文章(反向查询,按表名_set)
                                        tag_obj=models.Tag.objects.get(title="web开发")
                                        article_list=tag_obj.article_set.all()
                                        for i in article_list:
                                            print(i.title)                            
                                            
                                     实例5:查询web开发的这个标签对应的所有的文章对应的作者名字
                                          tag_obj=models.Tag.objects.get(title="web开发")
                                          article_list=tag_obj.article_set.all()    
                                          for article in article_list:
                                               print(article.user.username)
    基于QuerySet跨表查询 ( 正向查询,按字段 ;反向查询,按表名)
    
    
    一对多的查询:
    
     实例1:查询主键为4的文章的分类名称
    
    models.Article.objects.filter(nid=4).values("category__title") 
     models.Category.objects.filter(article__nid=4).values("title")
    
    实例2:查询用户yuan发表过的所有的文章
    models.UserInfo.objects.filter(username="yuan").values(article__title)
     models.Article.objects.filter(user__username="yuan").values("title")
    
    
    多对多的查询:
    
     实例3:查询主键为4的文章的标签名称(正向查询,按字段) 
    models.Article.objects.filter(nid=4).values("tags__title")
     models.Tag.objects.filter(article__nid=4).values("title") 
    
    实例4:查询web开发的这个标签对应的所有的文章(反向查询,按表名_set)
    models.Article.objects.filter(tags__title="web开发").values("title")
     models.Tag.objects.filter(title="web开发").values("article__title") 
    
    实例5:查询web开发的这个标签对应的所有的文章对应的作者名字
    models.Tag.objects.filter(title="web开发").values("article__user__username")
     models.UserInfo.objects.filter(article__tags__title="web开发").values("username")

             

  • 相关阅读:
    Code Forces Gym 100886J Sockets(二分)
    CSU 1092 Barricade
    CodeChef Mahesh and his lost array
    CodeChef Gcd Queries
    CodeChef GCD2
    CodeChef Sereja and LCM(矩阵快速幂)
    CodeChef Sereja and GCD
    CodeChef Little Elephant and Balance
    CodeChef Count Substrings
    hdu 4001 To Miss Our Children Time( sort + DP )
  • 原文地址:https://www.cnblogs.com/mengqingjian/p/8455303.html
Copyright © 2011-2022 走看看