rwx
rwx

Reputation: 716

What's the advantage of using namespace?

General

I read about namespace and tried it, but I don't get the point: What's the advantage of using namespaces.

What I do with namespaces

I include my app in the projects urls.py by

url(r'^myapp/', include('myapp.urls', namespace='myapp')),

In the apps urls.py I have

url(ur'^$', index, name='index'),

In the apps templates I can set a link by

<a href="{% url 'myapp:index' %}">

Problem

If I would share 'myapp' as an reuseable app I would force the user to include the app with the given namespace 'myapp'. Why shouldn't I just name the urls name 'myapp-index' instead of 'index' in urls.py?

url(ur'^$', index, name='myapp-index'),

and in the template

<a href="{% url 'myapp-index' %}">

Upvotes: 0

Views: 168

Answers (1)

renno
renno

Reputation: 2827

If you name your url with 'myapp-index', this is a kind of name-spacing. The namespaces feature in Django is to help with best practices and have a standard. Standards are helpful to visualize patterns.

You don't have to use namespaces, but it's helpful. You use to avoid conflicts with third party apps (or even multiple apps within your project).

You can read more in https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces

Upvotes: 1

Related Questions