django - 获取请求中的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"),
解决方案
这是我需要的代码
id = request.GET.get('<id>')
推荐阅读
- c++ - 按值、引用和右值传递字符串
- python - 如何**防止** Scrapy Request 下载请求?
- javascript - 由于异步功能,Tabris.js 游乐场示例不起作用
- java - 如何修改集合反序列化器,使其生成按给定条件过滤掉的集合?
- image - 如何通过 Jenkins 容器从 docker hub 中提取图像
- php - 未定义列:7 错误:列不存在
- javascript - 两个数组的水平串联
- javascript - 将数据发送到另一个页面 - html
- svn - 用于避免提交大于 100MB 的预提交脚本
- c++ - 通过this指针访问时的成员与自身的成员指针的区别(简单QT示例问题)