Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django HttpResponseRedirect reverse function in tutorial

Can someone please explain what is going on here with the Django Tutorial Part 4

Specifically, how the map function is working?

I understand that URLs shouldn't be hardcoded in the view functions.

return HttpResponseRedirect(reverse('mysite.polls.views.results', args=(p.id,)))
like image 701
geejay Avatar asked Feb 28 '23 09:02

geejay


1 Answers

The reverse function has access to the URL map that Django uses to find a view function for incoming URLs. In this case, you pass in a view function, and the arguments it will get, and it finds the URL that would map to it. Then the HttpResponseRedirect function creates a response that directs the browser to visit that URL.

This is a way of saying, "Now call the mysite.polls.views.results view."

like image 107
Ned Batchelder Avatar answered Mar 07 '23 22:03

Ned Batchelder