我有一个名为calculator.py文件允许的误差,其中有一个函数调用计算值(),它有一个方法称为计算:calculator.pyHTTP 405方法不仅在Django
class calc():
@classmethod
def calculate(request_data):
# start = time.time()
results = {}
a = 1
return a
我需要给一个POST在视图中请求py,并且应该在POST请求中显示calculator.py中的变量a(即1)。下面是我的views.py
from django.shortcuts import render
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.clickjacking import xframe_options_exempt
from rest_framework.decorators import api_view
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from calculator import calc
import json
# local variables
debug = True
@xframe_options_exempt
@csrf_exempt
@api_view(['POST'])
def calculated(request):
report_table = calc.calculate(data=request.data)
return JsonResponse(report_table.data, status=status.HTTP_201_CREATED)
#return JsonResponse({"data":report_table.data})
但是当我运行这个程序并检查POST请求的API终点,它显示了以下错误的截图..! API End point result
GET /api/calculated
HTTP 405 Method Not Allowed
Allow: OPTIONS, POST
Content-Type: application/json
Vary: Accept
{
"detail": "Method \"GET\" not allowed."
}
有人可以帮助我,为什么我收到此错误。这段代码有什么问题..? 我想要值'1',即在calculate.py中返回以显示在API端点中。有人可以帮我弄这个吗..!?请原谅我,如果我使用任何术语错误,因为我是新的Django框架。谢谢。
如果我使用@api_view(['POST','GET'])'那么我需要使用另一个条件以及对于POST方法,但我只是将其POST,并且只需要看看POST方法是否正在从'calculator.py'文件的'calc'类中访问'calculate'方法。有没有办法看到..? –