• Python


    request.method:

    获取请求的方法,例如 GET、POST 等

    views.py:

    from django.shortcuts import render, HttpResponse
    
    # request 对象
    def test(request):
        print(request.method)
        return render(request, "test.html")
    

    访问页面

    可以通过 request.method 查看请求方式

    request.GET:

    用来获取 URL 里面的 GET 方式的参数

    views.py:

    from django.shortcuts import render, HttpResponse
    
    # request 对象
    def test(request):
        print(request.GET)  # 返回的是一个字典类型
        print(request.GET.get("id"))  # 通过 key 获取相对应的 value
        return render(request, "test.html")
    

    访问:http://127.0.0.1:8000/test/?id=2&username=admin&password=123456

    request.POST:

    用来获取 POST 提交过来的数据

    test.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>测试页面</title>
    </head>
    <body>
    
    <p>测试页面</p>
    
    <form action="/test/" method="post">
        <input type="text" name="username" value="">
        <input type="submit" name="提交">
    </form>
    
    </body>
    </html>
    

    views.py:

    from django.shortcuts import render, HttpResponse
    
    # request 对象
    def test(request):
        print(request.POST)  # 返回的是一个字典类型
        print(request.POST.get("username"))  # 通过 key 获取相对应的 value
        return render(request, "test.html")
    

    访问网页:

    提交

    request.body:

    请求体,byte 类型,request.POST 的数据就是从 body 里提取的

    views.py:

    from django.shortcuts import render, HttpResponse
    
    # request 对象
    def test(request):
        print(request.body)
        return render(request, "test.html")
    

    访问网页:

    提交:

    这两串是 “提交” 的 URL 编码

    request.path_info:

    获取用户请求的路径,不包含域名和 URL 参数

    from django.shortcuts import render, HttpResponse
    
    # request 对象
    def test(request):
        print(request.path_info)
        return render(request, "test.html")
    

    访问:http://127.0.0.1:8000/test/?id=2&username=admin

  • 相关阅读:
    通过filebeat收集并通过elsaticsearch的pipeline功能解析nginx访问日志
    markdown blog Typora+minio+upic图床改造
    spark 使用shc 访问hbase超时问题解决办法
    定制logstash-output-kafka 添加额外事务参数
    《机器学习十讲》第八讲总结
    寒假学习日报(二十四)
    《机器学习十讲》第七讲总结
    寒假学习日报(二十三)
    《机器学习十讲》第六讲总结
    寒假学习日报(二十二)
  • 原文地址:https://www.cnblogs.com/sch01ar/p/11271914.html
Copyright © 2020-2023  润新知