_reverse_with_prefix() argument after * must be an iterable, not int

╄→尐↘猪︶ㄣ 提交于 2020-01-23 07:58:35

问题


I have used Django's reverse multiple times in the past but getting this error today which doesn't seem intuitive enough to debug:

TypeError: _reverse_with_prefix() argument after * must be an iterable, not int

Here's the view where I am using it:

from django.urls import reverse

...
...
def show_scores_url(self, obj):
    scores_url = reverse('get_scores', args=(obj.pk))
    return format_html('<a href="' + scores_url + '">Scores</a>')

...
...

回答1:


As mentioned in this comment, putting a comma at the end of the args tuple fixes it.

scores_url = reverse('get_scores', args=(obj.pk,))

(As mentioned in this SO answer, trailing comma is required for single-item tuples to disambiguate defining a tuple or an expression surrounded by parentheses)

Alternatively, as mentioned in the docs, using a list would work fine:

scores_url = reverse('get_scores', args=[obj.pk])


来源:https://stackoverflow.com/questions/52575418/reverse-with-prefix-argument-after-must-be-an-iterable-not-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!