url()
name
parameter
"What is it? Where does it live?"
url()
is simply a function that returns a django.core.urlresolvers.RegexURLPattern
object, so passing in a name='careers_contact'
argument sets name
for that object. None of that is really relevant until this url(...)
is placed into a URLconf.
THEN, if we need the URL of a view, we can now get it by passing that name
into {% url 'careers_contact' %}
in templates or reverse('careers_contact')
in code and on the backend those functions will use the name
to map back to the correct URL.
Why do we need it?
We can reverse the Python Path to get the URL (ex. reverse(blog.views.home)
), so what's the point in using name
?
URL Naming and Namespacing allow for 3 things:
- A simple way to reverse URL match Class-Based Views. Though it is possible without it.
- A way to distinguish URL patterns using the same view and parameters.
- A way to differentiate URL names between apps.
(Click the links for an example of the issue and how naming/namespacing solves it)