reverse function

15 views
Skip to first unread message

Agnese Camellini

unread,
May 29, 2014, 6:46:21 AM5/29/14
to django...@googlegroups.com
Good morning everyone, i was using django as of django 0.9 and now i'm looking again in the tutorial and i have to say that a lot has changed. I undestand che basics (even if i cannot understan why in the tutorial they use the context and doesn'use any more "render_to_response") but however what i cannot understand is che function "reverse" which is used in the views, what is its use? San you explain it to me while i'm going through the tutorial again to understand? Thank you. Agnese

Kelvin Wong

unread,
May 29, 2014, 7:04:47 AM5/29/14
to django...@googlegroups.com
The functions reverse and reverse_lazy are useful for figuring out the paths from labels. If you or someone else changes the urlconf in the future, your app uses the new paths without difficulty.

If you have in your urls.py

url(r'^home/$', views.home, name='home'),
url(r'^specifics/(?P<poll_id>\d+)/$', views.detail, name='detail'),

Then in your views.py you have:

print reverse('home')
# prints /home/

print reverse('detail', args=[12])
# prints /detail/12/

As for render_to_response, it is still there and everybody still uses it.

K

Kelvin Wong

unread,
May 29, 2014, 7:22:50 AM5/29/14
to django...@googlegroups.com
Oooppps!

print reverse('detail', args=[12])
# prints /specifics/12/

K
Reply all
Reply to author
Forward
0 new messages