Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the function of the name parameter in django.urls.path?

Tags:

url

django

When creating a path inside a urls.py file, one often does this:

urlpatterns = [     path('foo/',views.FooView,name='bar'), ] 

I'm a beginner to Django, but I am yet to see the function of the name parameter inside the path function. What is this parameter for, and how can one use it effectively inside a Django-powered website? In short, what does this parameter do, and why is it important to include when creating a path? Thanks!

like image 457
Orangft Avatar asked Aug 19 '18 23:08

Orangft


People also ask

What is name parameter in Django urls?

It is just that django gives you the option to name your views in case you need to refer to them from your code, or your templates. This is useful and good practice because you avoid hardcoding urls on your code or inside your templates.

What is the name field in path in Django?

FilePathField in Django Forms is a string field, for input of path of a particular file from server. It is used for select inputs from the user. One needs to specify which folders should be used in FilePathField and field displays the inputs in form of a select field. The default widget for this input is Select.

How the include function is used in the URL file in Django?

include() A function that takes a full Python import path to another URLconf module that should be “included” in this place. Optionally, the application namespace and instance namespace where the entries will be included into can also be specified.

How do I pass URL parameters in Django?

Django URL pass parameter to view You can pass a URL parameter from the URL to a view using a path converter. Then “products” will be the URL endpoint. A path converter defines which type of data will a parameter store. You can compare path converters with data types.


2 Answers

Sometimes you want to name views so that you can refer to them by the name rather than by the url. That way, if the url changes in your app, you only need to update the code in one place (the urls.py module). Existing code, which used the name rather than the url hardcoded, does not need to be updated.

For example, the reverse utility function accepts this name and returns the url:

from django.urls import reverse reverse('bar') 

If you do not make use of the name, moving a route is cumbersome - you need to find and update the urls throughout all the templates and the code.

like image 70
wim Avatar answered Sep 18 '22 07:09

wim


Imagine that you need to write a URL to refer to your FooView, then you can write it like

<a href="/foo/">link</a>

But a problem can occur if you later change your mind, and want to use qux/ for this view. So rewriting a URL, could result in a large amount of rewrites. It is also cumbersome if the URL contains parameters. Say you have a URL with a pk and type parameter (like foo/<pk>/qux/<type>), then you need to remember the format, and thus reason about the format, which is cumbersome: "is it /foo/1/qux/2 or /foo/2/qux/1?".

So we do not want to write these URL directly, we want to determine these: calculating the URL based on some identifier, and zero or more parameters. By defining a name in a path, we thus first need to define such identifier, and we can define a name for such path.

So now we can in the template use the {% url .. %} template tag instead, like:

<a href="{% url 'bar' %}">link</a>

Django will then do the math, and replace it with the correct URL. Not only does this save us a headache, the URL is also guaranteed to be correct (given of course the name itself is correct). So if it renders HTML output, we do not need to worry, that we might have written a typo in one of the URLs, since the {% url .. %} tag will raise an error if it can not find the name, or in case the parameters do not match.

We can do the same when we for example want to redirect to a page. If we have a certain view that redirects to bar, it looks like:

def redirect_view(request):     return redirect('bar')

and again, we are done. If later the URL changes, or when for example somebody wants to implemented translated URLs (for example you can run a server in English, and one in Russian), then these URLs will have the correct translation.

In case the URL contains a parameter, like for example:

urlpatterns = [     path('foo/<int:pk>/edit',views.FooView,name='bar'), ]

We can use this parameter as well. In the template it looks like:

<a href="{% url 'bar' pk=2 %}">link</a>

or in the view:

def redirect_view(request):     return redirect('bar', pk=2)

How that parameter fits in the URL, is not our problem. Django will find this out itself.

like image 21
Willem Van Onsem Avatar answered Sep 19 '22 07:09

Willem Van Onsem