【问题标题】:Django is not able to read the data posted from the POSTMAN?Django 无法读取 POSTMAN 发布的数据?
【发布时间】:2020-12-20 17:25:37
【问题描述】:

我有以下代码sn-p:-

from django.shortcuts import render
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
# Create your views here.

@csrf_exempt
def ajax_post_view(request):
    if request.method == "POST":
        foo = request.POST.get('Foo')
        print(foo)
        return JsonResponse(foo, safe=False)
    else:
        return JsonResponse("Not a POST method", safe=False)

我正在从 POSTMAN 发出一个 POST 请求,该请求显示空值作为响应:-

我错过了什么?

【问题讨论】:

  • 您是否尝试过 curl 命令只是为了确保它不是邮递员
  • curl -X POST -H "Content-Type: application/json" \ -d '{"Foo": "Bar", "email": "linuxize@example.com"}' \ localhost:8080/ajax_post_view
  • 上面的 curl 命令给了我 None
  • 你能分享你的urls.py吗?
  • 路径('ajax_post_view', core_views.ajax_post_view, name="ajax_post_view"),

标签: python django postman


【解决方案1】:

它不是一个合适的端点。你需要使用 Django rest framework 之类的东西

这将是最简单的方法 https://www.django-rest-framework.org/api-guide/views/#function-based-views

【讨论】:

  • 不使用 DRF 怎么办?
  • 或 django-tastypie
  • 我希望它在原始 django 中没有任何外部库!!
  • 那么你需要一个网页来调用它。
  • 你的意思是说没有网页我就不能做吗?
猜你喜欢
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-07
  • 2019-03-29
  • 1970-01-01
相关资源
最近更新 更多