Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

django - A real example of URL Namespace

I am studying the Django documentation, but I encountered a part that I cannot understand: what is a real example of how use to use a namespace in a real problem. I know the syntax but I do not know the purpose of this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...