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?
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?
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.