3

I am new to python and Django and am trying to determine how the following code works:

return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))

Specifically the 2nd argument of the reverse function. It looks like it is setting the parameter of args equal to a tuple. Why do I need an extra comma?

0

2 Answers 2

7

(p.id) is just p.id in parentheses, (p.id,) is a single-element tuple.

Parenthesized forms in docs

Sign up to request clarification or add additional context in comments.

Comments

3

The trailing comma is required if a tuple only has one item to differentiate a tuple from stylistic parenthesis.

Similar questions:
Python tuple comma syntax rule
Why does adding a trailing comma after a string make it a tuple

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.