【问题标题】:How to DJANGO Web Token and Save Procces (POST)?如何 DJANGO Web 令牌和保存过程 (POST)?
【发布时间】:2020-08-31 09:54:12
【问题描述】:

我正在尝试在 views.py 上添加数据,我使用来自 POSTMAN 的 POST 方法在 JSON 正文上发送用户名和密码参数。 正常情况下该功能成功运行如下:

json_str=((request.body).decode('utf-8'))
    json_data = json.loads(json_str)
    new_device = Device(id=json_data["id"], status=json_data["status"])
    try:
        new_device.save()
        resp_data = {
        'code': 200,
        "success": True
    }
        return JsonResponse(resp_data)
    except Exception as e:
        error_data = {
            "success": False,
            "error": str(e)
            }

        return JsonResponse(error_data)

在我的项目上启用 djangorestframework_simplejwt 包并成功运行后,我更改了 urls.py 链接,如: path('add/', add_device)path('add/', add_device.as_view()) 在views.py上:

class add_device(APIView):
    permission_classes = (IsAuthenticated,)
    
    def get(self,request):
        
        json_str=((request.body).decode('utf-8'))
        json_data = json.loads(json_str)
        new_device = Device(id=json_data["id"], status=json_data["status"])
        try:
            new_device.save()
            resp_data = {
            'code': 200,
            "success": True
        }
            return JsonResponse(resp_data)
        except Exception as e:
            error_data = {
                "success": False,
                "error": str(e)
                }

            return JsonResponse(error_data)

但现在,总是有 json 错误消息返回给我:

{
    "detail": "Method \"POST\" not allowed."
}

我该如何解决?

感谢大家的帮助..

【问题讨论】:

    标签: django web token


    【解决方案1】:

    我找到了解决办法,用了:

    def post(self, request, *args, **kwargs):

    它有效。

    【讨论】:

      猜你喜欢
      • 2018-11-16
      • 1970-01-01
      • 2020-12-29
      • 2015-07-01
      • 2012-05-24
      • 2012-12-19
      • 2021-12-15
      • 2021-03-01
      • 2020-11-12
      相关资源
      最近更新 更多