如何使用 python reverse() 函数将 id 列表作为查询参数传递?

How do I use python reverse() function to pass list of ids as query parameter?

我需要生成一个 URL 对象 ID 列表作为查询参数。 IE Url 看起来像 admin/app/model/?id__in=1,2,3。 我用过 reverse('admin:app_model_changelist', kwargs={id__in:<list_of_ids>}) 似乎不起作用

reverse() 不会生成查询参数。您可以尝试将字符串格式设置为

path = reverse('admin:app_model_changelist')
<b>result = '%s?id__in=%s' % (path, ','.join(map(str,list_of_ids))) </b>

使用 kwargs,您将必须提供带有命名参数的字典,如您的 URL 配置中所指定。

尝试使用 args,如 resolve() 上的 django documentation 中所述:

If the URL accepts arguments, you may pass them in args. For example:

from django.urls import reverse

def myview(request):
   return HttpResponseRedirect(reverse('arch-summary', args=[1945]))