【问题标题】:Why I send data to Django POST but fetch them in GET?为什么我将数据发送到 Django POST 但在 GET 中获取它们?
【发布时间】:2020-08-23 23:24:24
【问题描述】:

我是 Django 新手,今天构建了一个简单的测试。

def login_web(request):
    request.encoding = "utf-8"
    print("POST type ", request.method)
    print("body : ", request.body)
    print("POST : ", request.POST)
    print("GET : ", request.GET)
    username = request.POST.get("username")
    password = request.POST.get("password")
    print(username)
    print(password)

    user = auth.authenticate(username=username, password=password)
    if user is not None and user.is_active:
        print("YR1")
        auth.login(request, user)
        return JsonResponse({"foo": "bar1"})
    else:
        print("IM2")
        return JsonResponse({"foo": "bar2"})

我使用 Postman 向它发送 post 请求。

但结果很混乱。

POST type  POST
body :  b''
POST :  <QueryDict: {}>
GET :  <QueryDict: {'username': ['chivier'], 'password': ['123456']}>
None
None
IM2

我用request.method查了一下,确定我发送了一个POST请求。

我应该让它们出现在 request.POST 中,但为什么它们出现在 request.GET 中。

【问题讨论】:

    标签: django python-3.x postman


    【解决方案1】:

    这是因为您以query parameters 发送数据。查询参数始终可以通过request.GET 访问,它不依赖于请求类型。发送请求体中的数据,并通过request.POST访问。

    【讨论】:

      猜你喜欢
      • 2018-10-11
      • 2018-03-27
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2016-07-31
      • 2023-02-23
      • 2010-11-18
      • 1970-01-01
      相关资源
      最近更新 更多