not clear about the name= part of url rooting

28 views
Skip to first unread message

kk

unread,
Dec 13, 2015, 12:45:31 PM12/13/15
to django...@googlegroups.com
hello all,
I was going through the basic tutorial for django 1.8
I see in the 3rd part on views this line.
url(r'^(?P<question_id>[0-9]+)/$', views.details, name='detail'),
I have not understood the name = 'details' part?
is 'details' a parameter that will have a input value?
I am confused because we have already associated the views.detail
function to the root.
Can some one explain?
happy hacking.
Krishnakant.

knbk

unread,
Dec 13, 2015, 1:03:54 PM12/13/15
to Django users
Django allows you to reconstruct urls with the reverse() function and {% url %} template tag. You can reverse the url for a view by passing the view function itself, but this is not very practical. By setting a name for a view, you can reverse the url by referencing this name instead. This allows you to easily construct links to other pages on your site. For example, if question is an instance of your Question model, you can link to it in a template like this:

<a href="{% url 'detail' question_id=question.id %}">{{ question.title }}</a>

The {% url %} tag will generate a link to the 'detail' view for this question. 
Reply all
Reply to author
Forward
0 new messages