zoukankan      html  css  js  c++  java
  • 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:projectprojectsettings.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>
  • 相关阅读:
    1691: [Usaco2007 Dec]挑剔的美食家
    poj2892 Tunnel Warfare
    P3369 【模板】普通平衡树 Treap
    Splay的用法
    P3165 [CQOI2014]排序机械臂
    P2286 [HNOI2004]宠物收养场
    P3391 【模板】文艺平衡树(Splay)新板子
    3224: Tyvj 1728 普通平衡树(新板子)
    P2515 [HAOI2010]软件安装
    CF 219 D:Choosing Capital for Treeland(树形dp)
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/10356746.html
Copyright © 2011-2022 走看看