Namespacing

NameSpacing

Namespacing (What)

Namespacing is the process of keeping names local to an entity (subroutine, module, directory, or such) to avoid the problems that arise when the same name is used for two or more different things.

In Django, namespacing refers to keeping names local to an app.

Namespacing is a best practice in Django and in coding in general.

Namespacing (Why)

  1. If you don’t namespace, it is inevitable that you will use the same name for multiple things and this will cause coding errors that are hard to find.

  2. If you do namespace, your apps and packages can be used in multiple projects. That is, your code is modular.

  3. If you do namespace, you can use simpler names because you don’t have to worry about potential naming errors.

Namespacing (How)

In Django, namespace URL patterns, templates, and static files. Here is how

1. URL Pattern Namespacing

Django supports namespacing of URL patterns using the app_name attribute in urls.py and the namespace argument in include().

Step 1: Define app_name in urls.py of Each App

Each Django app should define its own app_name in urls.py:

Example: blog/urls.py

from django.urls import path
from . import views

app_name = "blog"  # This is the namespace for the app

urlpatterns = [
    path("", views.index, name="index"),
    path("post/<int:id>/", views.post_detail, name="post_detail"),
]

Example: shop/urls.py

from django.urls import path
from . import views

app_name = "shop"  # This is the namespace for the app

urlpatterns = [
    path("", views.index, name="index"),
    path("product/<int:id>/", views.product_detail, name="product_detail"),
]

Step 2: Include Namespaced URLs in the Project’s urls.py

Now, include these URLs in the project’s main urls.py:

project/urls.py

from django.urls import path, include

urlpatterns = [
    path("blog/", include("blog.urls", namespace="blog")),
    path("shop/", include("shop.urls", namespace="shop")),
]

Step 3: Reverse URL Lookup with Namespaces

When using reverse() or {% url %} in templates, specify the namespace:

Using reverse() in Views

from django.urls import reverse
from django.http import HttpResponseRedirect

def go_to_blog(request):
    return HttpResponseRedirect(reverse("blog:index"))  # Redirects to blog index

def go_to_shop(request):
    return HttpResponseRedirect(reverse("shop:index"))  # Redirects to shop index

Using {% url %} in Templates

<a href="{% url 'blog:index' %}">Go to Blog</a>
<a href="{% url 'shop:index' %}">Go to Shop</a>

2. Template Namespacing

To avoid template name conflicts, store templates in namespaced directories.

Example: Organizing Templates for Apps

blog/ templates/ blog/ index.html post_detail.html shop/ templates/ shop/ index.html product_detail.html

Referencing Templates in Views

def index(request): return render(request, “blog/index.html”)

def shop_index(request): return render(request, “shop/index.html”)

3. Static Files Namespacing

If you have multiple apps with static files (CSS, JavaScript, images), namespace them to avoid conflicts.

Organizing Static Files

blog/ static/ blog/ style.css shop/ static/ shop/ style.css

Referencing Static Files in Templates

{% load static %}
<link rel="stylesheet" href="{% static 'blog/style.css' %}">
<link rel="stylesheet" href="{% static 'shop/style.css' %}">

Summary