zoukankan      html  css  js  c++  java
  • 运维开发笔记整理-QueryDict对象

                运维开发笔记整理-QueryDict对象

                                       作者:尹正杰

    版权声明:原创作品,谢绝转载!否则将追究法律责任。

      客户端发送数据请求有很多种,相信运维人员已经很清楚了,如果不太清楚的话可以参考我之前的学习笔记:http原理详解

    一.GET与POST请求

      在HttpRequest 对象中,GET 和POST 属性是django.http.QueryDict 的实例,它是一个自定义的类似字典的类,用来处理同一个键带有多个值。我们可以编写代码,查看GET和POST返回的数据请求到底是什么,具体代码如下:

    #!/usr/bin/env python
    #_*_conding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    
    from django.http import HttpResponse
    
    def index(request):
        print("请求的方法是:",request.method)
        print("GET的数据是:",request.GET)
        print("POST的数据是:", request.POST)
        return HttpResponse("")

    二.QueryDict对象

    1>.QueryDict对象不可修改,只能通过修改副本的方式进行修改!

        第一:在HttpRequest 对象中,GET 和POST 属性是django.http.QueryDict 的实例,它是一个自定义的类似字典的类,用来处理同一个键带有多个值。这个类的需求来自某些HTML 表单元素传递多个值给同一个键。
            
       
      第二:request.POST 和request.GET 的QueryDict 在一个正常的请求
    /响应循环中是不可变的。若要获得可变的版本,需要使用.copy()。

    2>.获取请求的参数(注意get和getlist的用法)

    #!/usr/bin/env python
    #_*_conding:utf-8_*_
    #@author :yinzhengjie
    #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
    
    from django.http import HttpResponse
    
    def index(request):
        if request.method == "GET":
            print("使用get方法获取‘name’值:",request.GET.get("name"))          #获取元素列表的最后一个参数
            print("使用getlist方法‘hobby'值:",request.GET.getlist("hobby"))      #获取传来的整个列表
        elif request.method == "POST":
            print(request.POST)
        return HttpResponse("尹正杰到此一游!")

    3>.使用Django的Shell环境(ipython)实例化QueryDict

      使用“python manage.py shell”命令登录含有Django环境的shell,即ipython环境。

    C:UsersyinzhengjiesoftwaresPycharmProjectDevOps>python manage.py shell
    Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>>
    >>> from django.http import QueryDict
    >>>
    >>> QueryDict('name=yinzhengjie&age=26&hobby=Python&hobby=Java&hobby=Golang')
    <QueryDict: {'name': ['yinzhengjie'], 'age': ['26'], 'hobby': ['Python', 'Java', 'Golang']}>
    >>>
    >>> QueryDict.fromkeys(['a','a','b'],value='A')
    <QueryDict: {'a': ['A', 'A'], 'b': ['A']}>
    >>>
    >>>

     

    4>.QueryDict常用方法汇总

     QueryDict.setdefault(key, default=None)[source]
     QueryDict.update(other_dict)
     QueryDict.items()
     QueryDict.values()
     QueryDict.copy()
     QueryDict.getlist(key, default=None)
     QueryDict.setlist(key, list_)[source]
     QueryDict.appendlist(key, item)
     QueryDict.setlistdefault(key, default_list=None)
     QueryDict.lists()
     QueryDict.pop(key)
     QueryDict.popitem()
     QueryDict.dict()
     QueryDict.urlencode(safe=None)

      更多关于QueryDict方法多使用,详情请参考:“https://docs.djangoproject.com/en/1.11/ref/request-response/#querydict-objects”。

    C:UsersyinzhengjiesoftwaresPycharmProjectDevOps>python manage.py shell
    Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>>
    >>> from django.http import QueryDict
    >>>
    >>> data = QueryDict('name=yinzhengjie&age=26&hobby=Python&hobby=Java&hobby=Golang')
    >>>
    >>> data.urlencode()
    'name=yinzhengjie&age=26&hobby=Python&hobby=Java&hobby=Golang'
    >>>
    >>> data.get('name')
    'yinzhengjie'
    >>>
    >>> data.getlist('hobby')
    ['Python', 'Java', 'Golang']
    >>>
    没事了,大家就多练习一下,熟悉一下就好。别到时前后端传输数据你都不知道咋取参数就尴尬了。 
  • 相关阅读:
    codeforces #586 ABC~D
    codeforces #585 div2 ABCD
    编译原理实验 NFA子集法构造DFA,DFA的识别 c++11实现
    codeforces #599 div2 ABCD
    codeforces #598 div3 ABCDF
    codeforces #587 div3 ABCDE
    codeforces educational round 73 div2 ABCD
    Mud Puddles ( bfs )
    2019牛客多校第十场B.Coffee Chicken(递归)
    2019牛客多校训练第七场A. String(暴力)
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/10246239.html
Copyright © 2011-2022 走看看