简体   繁体   English

向 Django web api 发起 GET 请求,并将 id 列表作为数组发送

[英]Initiating a GET request to Django web api and send a list of ids as an array

I'm trying to call a rest API in django and send a list of Ids as a paremeter (I can't use query string).我正在尝试在 django 中调用 rest API 并发送 Id 列表作为参数(我不能使用查询字符串)。

This works if I send only one, but how to specify it for an array of integers:如果我只发送一个,这有效,但是如何为整数数组指定它:

path('details/<int:id>/', views.get_details),

def get_details(request, id=0)

What if I have a method that will take a list of Ids:如果我有一个可以获取 Id 列表的方法怎么办:

def get_data(request, ids=[])

How should the url be defined so that the ids array is populated?应该如何定义 url 以便填充 ids 数组?

You can parse ids list manually like this您可以像这样手动解析 ids 列表


path('details/<ids>/', views.get_details),

def parse_ids(ids: str):
    result = []
    for id in ids.split(','):
        try:
            result.append(int(id))
        except ValueError:
            pass
    return result

def get_details(request, ids):
    id_list = parse_ids(ids)
    # do stuff
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM