Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJonathan Wadsworth
20,196 PointsCould you explain the reverse function in more detail?
I'm still quite confused by the reverse function. I read the extra docs in the link you provided, but the examples were very minimal and each was completely different. Could you walk through several examples in detail where you show what the return value of reverse() is each time. I'd love to know when each of the parameters would be used and what effect they might have on the same url.
1 Answer
Ratul Minhaz
3,331 PointsThis reverse() function in Django takes a name of any URL and returns its path. Say we have an url named 'news-archive', with the end point 'archive/', defined like this:
url(r'^archive/$', views.archive, name='news-archive')
If we do a Django reverse() with the name of the url like this:
reverse('news-archive')
It will give us the url 'archive/' in return. The most used parameter of this function is probably 'args'. When you want an url that is define like this:
url(r'^articles/([0-9]{4})/$', views.year_archive, name='news-year-archive'),
In these sort of urls you would need to provide the url name as well as arguments:
reverse('news-year-archive', args=(2016,))
This piece of code will return 'articles/2016'.
I hope this explains a bit. Sorry, couldn't explain in depth since I am new to Django just like you.