Much like module namespacing, they allow you to group subitems in more
logical and descriptive ways while preventing naming collisions. So, if a
Django project has a blog, with a list of Posts at /blog/posts/, a sensible
url name for that pattern might be "post_list" (and using url names in your
application is a good thing). Now, say the same project also exposes a
public API, which includes includes Posts at the endpoint
/api/v1/blog/posts/. Since using url names is a good thing, what would you
name the API endpoint while not clobbering the already-defined 'post_list'
url name? Sure, something like 'api_post_list' might be acceptable for the
endpoint I just described, but I prefer the namespaced version
'api:post_list' for a few reasons:

1) You can define a namespace one time when you include a collection of URL
patterns into the top-level urls.py
2) Because you only have to do this once, changing namespaces is a matter
of changing one thing in one place
3) each url pattern in the namespace url pattern collection has no idea
it's even been namespaced, which means you can go on defining the url names
without having to remember some convention for preventing naming collisions
(since django takes care of the for you by following the namespace:url_name
pattern across the board).

Admittedly, this isn't very useful on small projects, but if you encounter
a situation like the one above (or your project grows very large), then the
potential for naming collisions very quickly becomes a reality.

Hope this helps,
Jonathan


On Wed, Mar 5, 2014 at 4:25 AM, anubhav joshi <anubhav9...@gmail.com> wrote:

> Can anyone explain how URL Namespaces work in django.
> Some examples would be great.
> (Apart from those in the docs.)
>
> Regards,
> Anubhav Joshi
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/1d59cdb2-d375-493d-88ae-da2ece757eee%40googlegroups.com<https://groups.google.com/d/msgid/django-users/1d59cdb2-d375-493d-88ae-da2ece757eee%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPMFOb68DHThkFMW2mPtNPLW2cs4pA4XOVcYi-PygPpWCvgkAw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to