URL命名空间的一个真实例子 [英] A real example of URL Namespace
问题描述
我正在研究Django文档,但我必须考虑到我无法理解解决问题的一个问题,文档不清楚。任何知道的人,你能否提供一个真正的例子,说明如何在真正的问题中使用命名空间。我知道语法,但我不知道这个的目的..我不明白。谢谢
通常,它们用于将每个应用程序的URL放入自己的命名空间。这样可以防止 reverse()
Django函数和 {%url%}
模板函数返回错误的URL,因为URL模式名称在另一个应用程序中匹配。
我在项目级别 urls.py
文件如下:
from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^ $','main.views。 main',name ='main'),
url(r'^ login $','django.contrib.auth.views.login',name =login),
url(r' ^ logout $','django.contrib.auth.views.logout',
{next_page:/},name =logout),
#Admin
url(r'^ admin / doc /',include('django.contrib.admindocs.urls')),
url(r'^ admin /',include(admin.site.urls)),
)
#自动添加应用程序。
for app in settings.LOCAL_APPS:
urlpatterns + = patterns('',
url(r'^ {0} /'。format(app),include(app +'.urls ',namespace = app)),
)
注意最后一节:我已经安装的应用程序( settings.LOCAL_APPS
是我添加的仅包含我的应用程序的设置;它被添加到 INSTALLED_APPS
其他东西,如南),在每个都找到一个 urls.py
,并将这些URL导入到应用程序后命名的命名空间中,并将这些URL进入一个以应用程序命名的URL子目录。
所以,例如,如果我有一个名为 hosts
的应用程序,和 hosts / urls.py
看起来像:
from django.conf .urls.defaults import *
urlpatterns = patterns('hosts.views',
url(r'^ $','show_hosts',name ='list'),
)
现在我的 views.py
可以调用 reverse(hosts:list)
获取调用 hosts.views.show_hosts
的页面的URL ,它将看起来像/ hosts /
。模板中的 {%urlhosts:list%}
也是如此。这样我就不必担心在另一个应用程序中与一个名为list的URL冲突,而且我不必在 hosts _
之前的每个名称前缀。 p>
请注意,登录页面位于 {%urllogin%}
,因为它没有给出命名空间。
I am studying the Django documentation but I gotta into a part that I can not understand what kind of problem it is resolving and the documentation is not clear to me. Anyone that knows, could you please provide me a real example of how use namespace in a real problem. I know the syntax but I do not know the purpose of this.. I could not understand. Thanks
Typically, they are used to put each application's URLs into their own namespace. This prevents the reverse()
Django function and the {% url %}
template function from returning the wrong URL because the URL-pattern name happened to match in another app.
What I have in my project-level urls.py
file is the following:
from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'main.views.main', name='main'),
url(r'^login$', 'django.contrib.auth.views.login', name="login"),
url(r'^logout$', 'django.contrib.auth.views.logout',
{"next_page": "/"}, name="logout"),
# Admin
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
# Auto-add the applications.
for app in settings.LOCAL_APPS:
urlpatterns += patterns('',
url(r'^{0}/'.format(app), include(app + '.urls', namespace=app)),
)
Note the last section: this goes through the applications I have installed (settings.LOCAL_APPS
is a setting I added that contains only my apps; it gets added to INSTALLED_APPS
which has other things like South), looks for a urls.py
in each of them, and imports those URLs into a namespace named after the app, and also puts those URLs into a URL subdirectory named after the app.
So, for example, if I have an app named hosts
, and hosts/urls.py
looks like:
from django.conf.urls.defaults import *
urlpatterns = patterns('hosts.views',
url(r'^$', 'show_hosts', name='list'),
)
Now my views.py
can call reverse("hosts:list")
to get the URL to the page that calls hosts.views.show_hosts
, and it will look something like "/hosts/"
. Same goes for {% url "hosts:list" %}
in a template. This way I don't have to worry about colliding with a URL named "list" in another app, and I don't have to prefix every name with hosts_
.
Note that the login page is at {% url "login" %}
since it wasn't given a namespace.
这篇关于URL命名空间的一个真实例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!