django HttpRequest对象

        概述:
            服务器接收http请求后,会根据报文创建HttpRequest对象
            视图的第一个参数就是HttpRequest对象
            django创建的,之后调用视图时传递给视图
        属性
            path:请求的完整路径(不包括域名和端口)
            method:表示请求的方式,常用的有GET,POST
            encoding:表示浏览器提交的数据的编码方式,一般为utf-8
            GET:类似于字典的对象,包含了get请求的所有参数
            POST:类似于字典的对象,包含了post请求的所有参数
            FILES:类似字典的对象,包含了所有上传的文件
            COOKIES:字典,包含所有的cookie
            session:类似字典的对象,表示当前会话
        方法
            is_ajax():如果是通过XMLHttpRequest发起的,返回 True
        QueryDict对象
            request对象中的GET,POST都属于QueryDict对象
            方法:
                get():
                    根据键获取值,只能获取一个值
                    www.sunck.wang/abc?a=1&b=2&c=3
                getlist()
                    将键的值以列表的形式返回
                    可以获取多个值
                    www.sunck.wang/abc?a=1&b=2&c=3
        GET属性
            获取浏览器传递过来数据
            www.sunck.wang/abc?a=1&b=2&c=3
            urls.py
            url(r'^get1', views.get1),   #结尾不能加$,否则无法匹配
            views.py
            def get1(request):
                a = request.GET.get('a')
                b = request.GET.get('b')
                c = request.GET.get('c')
                return HttpResponse(a + " " + b + " " + c)

            www.sunck.wang/abc?a=1&a=2&c=3
            urls.py
            url(r'^get2', views.get2),
            views.py
            def get2(request):
                a = request.GET.getlist('a')
                a1 = a[0]
                a2 = a[1]
                c = request.GET.get('c')
                return HttpResponse(a1 + " " + a2 + " " + c)
        POST属性
            使用表单模拟POST请求
            关闭CSRF:project\project\settings.py
                MIDDLEWARE = [
                    'django.middleware.security.SecurityMiddleware',
                    'django.contrib.sessions.middleware.SessionMiddleware',
                    'django.middleware.common.CommonMiddleware',
                    # 'django.middleware.csrf.CsrfViewMiddleware',
                    'django.contrib.auth.middleware.AuthenticationMiddleware',
                    'django.contrib.messages.middleware.MessageMiddleware',
                    'django.middleware.clickjacking.XFrameOptionsMiddleware',
                ]
            示例:
                def showregist(request):
                    return render(request, 'myApp/regist.html',)

                def regist(request):
                    name = request.POST.get("name")
                    gender = request.POST.get("gender")
                    age = request.POST.get("age")
                    hobby = request.POST.getlist("hobby")
                    print(name)
                    print(gender)
                    print(age)
                    print(hobby)
                    return HttpResponse("regist")
            路径:
                url(r'^showregist/$', views.showregist),
                    url(r'^showregist/regist/$', views.regist),
            页面:
                   <!doctype html>
                <html lang="en">
                <head>
                    <meta charset="UTF-8">
                    <meta name="viewport"
                          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
                    <meta http-equiv="X-UA-Compatible" content="ie=edge">
                    <title>注册</title>
                </head>
                <body>
                    <form action="regist/" method="post">
                        姓名:<input type="text" name="name" value=""/>
                        <hr>
                        性别:<input type="radio" name="gender" value="1">男<input type="radio" name="gender" value="0">女
                        <hr>
                        爱好:<input type="checkbox" name="hobby" value="power"/>权利<input type="checkbox" name="hobby" value="money">金钱<input type="checkbox" name="hobby" value="beauty">美女<input type="checkbox" name="hobby" value="Tesla">Tesla
                        <hr>
                        <input type="submit" value="注册">
                    </form>
                </body>
                </html>
posted @ 2019-02-08 21:00  超级学渣渣  阅读(593)  评论(0编辑  收藏  举报