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!
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With