reverse function

瀏覽次數:15 次
跳到第一則未讀訊息

Agnese Camellini

未讀,
2014年5月29日 清晨6:46:212014/5/29
收件者: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

未讀,
2014年5月29日 清晨7:04:472014/5/29
收件者: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

未讀,
2014年5月29日 清晨7:22:502014/5/29
收件者:django...@googlegroups.com
Oooppps!

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

K
回覆所有人
回覆作者
轉寄
0 則新訊息