*之后的_reverse_with_prefix()参数必须是可迭代的,而不是int [英] _reverse_with_prefix() argument after * must be an iterable, not int

查看:116
本文介绍了*之后的_reverse_with_prefix()参数必须是可迭代的,而不是int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去多次使用Django的 reverse ,但今天却收到此错误,似乎调试起来还不够直观:

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

这是我使用它的视图:

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>')

...
...


推荐答案

注释,在args元组的末尾添加了一个逗号来解决该问题。

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,))

(如 SO答案,单项元组必须使用逗号逗号来消除定义元组或括号中的表达式的歧义)

(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])

这篇关于*之后的_reverse_with_prefix()参数必须是可迭代的,而不是int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆