Django从视图调用API并嵌套POST

时间:2018-02-15 04:41:42

标签: python django django-rest-framework

我正在尝试从另一个项目进入api的POST方法。

首先,我发帖子来创建约会。如果预约成功,我会将当前用户配置文件从另一个项目发布到患者表中。

如何在POST中执行POST以及如何发布当前用户信息? 我尝试这样做,如下面的代码所示,但它只是坏了。

我发布在患者表中的字段 我的用户个人资料字段--->病人表
userId> patientId
first_name> first_name
last_name> last_name
email> email

这是我的代码:

观点:

@csrf_exempt
def my_django_view(request):

    # request method to api from another project
    if request.method == 'POST':
        r = requests.post('http://127.0.0.1:8000/api/makeapp/', data=request.POST)
    else:
        r = requests.get('http://127.0.0.1:8000/api/makeapp/', data=request.GET)

    if r.status_code == 201 and request.method == 'POST':
        data = r.json()
        print(data)

        # Have to post the user profile information into Patient table in another project.
        user_profile_attrs = {
            "patientId": self.request.userId,
            "first_name": self.request.first_name,
            "last_name": self.request.last_name,
            "username": self.request.username,
            "email": self.request.email,
        }
        # save into patient table of another project
        save_profile = requests.post('http://127.0.0.1:8000/api/patient/', data=request.POST)


        return HttpResponse(r.text)
    elif r.status_code == 200:  # GET response
        return HttpResponse(r.json())
    else:
        return HttpResponse(r.text)

2 个答案:

答案 0 :(得分:3)

问题

  1. requests.get传递params中不在data中的参数。详情click here
  2. 使用request.POST.get('userId')从请求中获取数据,而不是request.userId
  3. ...

    @csrf_exempt
    def my_django_view(request):
    
        if request.method == 'POST':
            r = requests.post('http://127.0.0.1:8000/api/makeapp/', data=request.POST)
        else:
            r = requests.get('http://127.0.0.1:8000/api/makeapp/', params=request.GET)
    
        if r.status_code == 201 and request.method == 'POST':
            data = r.json()
            print(data)
    
            # Have to post the user profile information into Patient table in another project.
            user_profile_attrs = {
                "patientId": request.POST.get('userId'),
                "first_name": request.POST.get('first_name'),
                "last_name": request.POST.get('last_name'),
                "username": request.POST.get('username'),
                "email": request.POST.get('email'),
            }
            # save into patient table of another project
            save_profile = requests.post('http://127.0.0.1:8000/api/patient/', data=request.POST)
    
    
            return HttpResponse(r.text)
        elif r.status_code == 200:  # GET response
            return HttpResponse(r.json())
        else:
            return HttpResponse(r.text)
    

答案 1 :(得分:1)

一种方法是:

@csrf_exempt
def my_django_view(request):

# request method to api from another project
if request.method == 'POST':
    r = requests.post('http://127.0.0.1:8000/api/makeapp/', data=request.POST)
    if r.status_code == 201:
        data = r.json()
        print(data)

        # Have to post the user profile information into Patient table in another project.
        user_profile_attrs = {
            "patientId": request.POST.get('userId'),  # or request.user.id
            "first_name": request.POST.get('first_name'),  # or request.user.first_name
            "last_name": request.POST.get('last_name'),  # or request.user.last_name
            "username": request.POST.get('username'),  # or request.user.username
            "email": request.POST.get('email'),  # or request.user.email
        }
        # save into patient table of another project
        save_profile = requests.post('http://127.0.0.1:8000/api/patient/', data=request.POST)

        return HttpResponse(r.text)
else:
    r = requests.get('http://127.0.0.1:8000/api/makeapp/', data=request.GET)


if r.status_code == 200:  # GET response
    return HttpResponse(r.json())
else:
    return HttpResponse(r.text)

但您最好使用基于类的视图并拆分POSTGET请求。

另外,请删除self.,因为它没有任何意义。

[编辑:已更新以正确访问请求数据。如果用户信息未作为args传递,则需要注释访问。 由于@AneeshRS'而修改答案]