首页 > 解决方案 > 如何使用邮递员向 Django RestAPI 提供用户输入?

问题描述

我制作了一个 DjangoRestApi 并使用邮递员(POST 方法)提供用户输入。但错误是

TypeError: Object of type 'JSONDecodeError' is not JSON serializable 

它显示在我出错的 django 服务器中,请帮助谢谢

视图.py

import spacy
from django.shortcuts import render,HttpResponse
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from django.http import JsonResponse
from django.core import serializers
from django.conf import settings
import json  

nlp = spacy.load('en_core_web_sm')
@api_view(["POST"])
def nounphrases(requestdata):
    try:
        text = json.loads(requestdata.body)
        nounphrases = []
        for word in (nlp((text))):
            c = (word.lemma_)
            nounphrases.append(c)
            output = [{"nounphrases" : nounphrases  }]

        return JsonResponse(json.dumps(output))
    except ValueError as e:
        return Response(e,status.HTTP_400_BAD_REQUEST)

标签: pythonapidjango-rest-framework

解决方案


推荐阅读