我有一个Django休息框架APIView:
class MyAPIView(views.APIView):
def post(self, request):
field = request.POST.get("field")
print(field)
return Response({"field": field}, status=200)
我想使用Django API从单独的进程调用它,我是这样做的:
from django.http import HttpRequest, QueryDict
request = HttpRequest()
request.method = "POST"
request.POST = QueryDict(mutable=True)
request.POST["field"] = "5"
response = MyAPIView.as_view()(request=request)
但是当在MyAPIView中打印field
时,它总是None
。如何使用Django调用post方法?
1条答案
按热度按时间qxsslcnc1#
1.如果您需要从另一个视图调用视图-请选中此answers
1.如果需要向视图发送请求
pip install requests
或poetry add requests
django extensions
命令show_urls
用法:
python manage.py show_urls
1.如果你确实需要运行某个视图的代码,为什么你不能把这个代码移到某个函数/静态方法中,然后在视图和代码的其他部分调用它呢?