12

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

...
...
Anupam
  • 13,804
  • 17
  • 61
  • 90

2 Answers2

25

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 from 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])
Anupam
  • 13,804
  • 17
  • 61
  • 90
0

You can also try to use kwargs={'pk':models_saving_variable.pk}.

The final code will be:

return HttpResponseRedirect(reverse('app_name:view_name', kwargs={'pk': saving_variable.pk}))
aboger
  • 1,976
  • 6
  • 33
  • 42
The.lYNCAN
  • 131
  • 2
  • 3