首页 > 解决方案 > 获取请求中的id参数

问题描述

我是 Django 新手,一个小细节一直困扰着我。我有一个 api 端点,可以返回一位患者的详细信息。我已经成功提出了获取请求并在邮递员上进行了测试。它返回 id = 996 的特定患者的数据(我已经硬编码了 id)。但我需要设置它,以便它可以从邮递员的参数中选择 id,而不是这里的硬编码。如何设置参数并将它们附加到 url 上,以便我使用邮递员提供的 id 而不是硬编码?请协助

视图.py

class PatientDetailsView(GenericAPIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

@classmethod
@encryption_check
def get(self, request, *args, **kwargs):

    try:
        result = {}
        auth = cc_authenticate()
        res = getPatientDetails(auth["key"], id)
        result = res
       
        return Response(result, status=status.HTTP_200_OK)
            
    except Exception as e:
        error = getattr(e, "message", repr(e))
        result["errors"] = error
        result["status"] = "error"

    return Response(result, status=status.HTTP_400_BAD_REQUEST)

api_service.py

def getPatientDetails(auth, id):

print("getting patientdetails from Callcenter")

try:
    print(auth)
    # print(url)
    id= 996
    
    headers = {
                "Authorization": f'Token {auth}'
    }
    url = f'{CC_URL}/patients/v1/details/?id={id}'
    print(url)
    res = requests.get(url, headers=headers)
   
    print("returning patientdetails response",  res.status_code)

    return res.json()

except ConnectionError as err:
    print("connection exception occurred")
    print(err)

    return err   

网址.py

 path("details/", views.PatientDetailsView.as_view(), name="patient_info"),

标签: djangopython-requests

解决方案


这是我需要的代码

id = request.GET.get('<id>')

推荐阅读