如何在django函数中发出REST请求以发出REST请求? [英] How do I make a POST request from within a django function to rest api?

查看:54
本文介绍了如何在django函数中发出REST请求以发出REST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有xyz.py文件。在此文件中,我有一个函数

I have xyz.py file. In this file I have a function

@api_view(['POST']) 
def abc(request):

可以通过URL进行访问,例如 / algorithms / abc

which can be accessible at a url, say /algorithms/abc.

我还有另一个功能

def pkr():

我想从此功能向 abc(request)。我试过
requests.request('POST', / algorithms / abc,data = data_input) request.data <收到 abc(request)的/ code>作为QueryDict(& not dict)&松散 pkr()函数中 data_input 中存在的一些文本。

I want to make POST request from this function to abc(request). I tried requests.request('POST', "/algorithms/abc", data=data_input) but the request.data at abc(request) is received as QueryDict (& not dict) & looses some texts that existed in data_input inside pkr() function.

推荐答案

只是要澄清@Daniel_Roseman和@Hugo_Luis_Villalobos_Canto在评论中说的话。

Just to clarify what @Daniel_Roseman and @Hugo_Luis_Villalobos_Canto is saying in comment.

我们可以抽象(重构)逻辑以在一些单独的函数中处理数据,然后通过将数据传递给两个函数在两个地方重复使用同一函数。

We can abstract (refactor out) the logic to process the data in some separate function and then re-use the same function at both places by passing data to it.

类似这样的东西:

def process_data(data):
    # process your data here
    print(data)


@api_view(['POST']) 
def abc(request):
    data = request.POST
    process_data(data)


def pkr():
    process_data(data)

这篇关于如何在django函数中发出REST请求以发出REST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆