I was poking around my friends project and as I looked through the urls.py file i noticed this:
我正在寻找我的朋友项目,当我浏览urls.py文件时,我注意到了这一点:
url(r'^apply/$', contact.as_view(), name='careers_contact'),
I just recently learned about class based views, and it all makes sense to me except for the last bit name='careers_contact'
. I also can't seem to find the meaning of this online.
我刚刚学习了基于类的视图,除了最后一个名称='careers_contact'之外,这对我来说都是有意义的。我似乎也无法在网上找到这个含义。
Can someone shed light on what this is, where that name lives, and what its doing?
有人能否阐明这是什么,这个名字在哪里生活,它在做什么?
2 个解决方案
#1
9
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.
url()只是一个返回django.core.urlresolvers.RegexURLPattern对象的函数,因此传入name ='careers_contact'参数可以设置该对象的名称。在将此url(...)放入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.
那么,如果我们需要视图的URL,我们现在可以通过将该名称传递到模板中的{%url'careers_contact'%}或代码中的反向('careers_contact')来获得它,并且在后端这些函数将使用该名称映射回正确的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
?
我们可以反转Python Path来获取URL(例如reverse(blog.views.home)),那么使用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.
- 一种简单的反向URL匹配基于类的视图的方法。虽然没有它可能。
- A way to distinguish URL patterns using the same view and parameters.
- 一种使用相同视图和参数区分URL模式的方法。
- A way to differentiate URL names between apps.
- 一种区分应用程序之间URL名称的方法。
(Click the links for an example of the issue and how naming/namespacing solves it)
(单击链接以获取问题示例以及命名/命名空间如何解决问题)
#2
3
The reason they probably added a namespace for the URL is so that they can do reverse namespaced URL.
他们可能为URL添加名称空间的原因是他们可以执行反向命名空间URL。
For example in a template somewhere, you will probably see something like:
例如,在某个地方的模板中,您可能会看到如下内容:
<a href="{% URL 'contact:careers_contact' %}"> Click me! </a>
#1
9
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.
url()只是一个返回django.core.urlresolvers.RegexURLPattern对象的函数,因此传入name ='careers_contact'参数可以设置该对象的名称。在将此url(...)放入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.
那么,如果我们需要视图的URL,我们现在可以通过将该名称传递到模板中的{%url'careers_contact'%}或代码中的反向('careers_contact')来获得它,并且在后端这些函数将使用该名称映射回正确的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
?
我们可以反转Python Path来获取URL(例如reverse(blog.views.home)),那么使用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.
- 一种简单的反向URL匹配基于类的视图的方法。虽然没有它可能。
- A way to distinguish URL patterns using the same view and parameters.
- 一种使用相同视图和参数区分URL模式的方法。
- A way to differentiate URL names between apps.
- 一种区分应用程序之间URL名称的方法。
(Click the links for an example of the issue and how naming/namespacing solves it)
(单击链接以获取问题示例以及命名/命名空间如何解决问题)
#2
3
The reason they probably added a namespace for the URL is so that they can do reverse namespaced URL.
他们可能为URL添加名称空间的原因是他们可以执行反向命名空间URL。
For example in a template somewhere, you will probably see something like:
例如,在某个地方的模板中,您可能会看到如下内容:
<a href="{% URL 'contact:careers_contact' %}"> Click me! </a>