Name parameter in Django's URLconfs

2

In the URLconf below:

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

What is the function of the name parameter? Is it possible to explain with a practical example?

    
asked by anonymous 26.05.2017 / 01:58

1 answer

4

The name parameter is used to reference the URL. For example, if you want to point a link to the index in the template, you would use the following code:

<a href="{% url 'index' %}">Acessar o index</a>

In view , you can get the URL through reverse . In this example a redirect is done:

from django.http import HttpResponseRedirect
from django.urls import reverse

def redirecionar_para_o_index(request):
    return HttpResponseRedirect(reverse('index'))

At documentation you can see more details.

    
26.05.2017 / 02:51