Python
Django
ImproperlyConfiguredError
app_name
namespace

ImproperlyConfiguredError about app_name when using namespace in include

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

This Django error happens when you use namespace= in include() but the included URL configuration does not provide an app_name. Django needs both pieces so it can build a fully qualified URL namespace such as blog:detail.

Why Django Raises the Error

Suppose your project URL configuration looks like this:

python
1from django.urls import include, path
2
3urlpatterns = [
4    path("blog/", include("blog.urls", namespace="blog")),
5]

If blog/urls.py does not define app_name, Django cannot determine the application namespace for that included URLconf. The framework treats this as misconfiguration and raises ImproperlyConfigured.

Conceptually, namespace is the instance namespace, while app_name identifies the application namespace. When you provide a namespace manually, Django expects the included module to declare what app that namespace belongs to.

The Usual Fix: Add app_name to the Included URLconf

In the included urls.py, declare app_name at module level:

python
1from django.urls import path
2from . import views
3
4app_name = "blog"
5
6urlpatterns = [
7    path("", views.index, name="index"),
8    path("<int:post_id>/", views.detail, name="detail"),
9]

Now this works:

python
1from django.urls import include, path
2
3urlpatterns = [
4    path("blog/", include("blog.urls", namespace="blog")),
5]

And in templates or code, namespaced reversing works as expected:

python
from django.urls import reverse

print(reverse("blog:detail", kwargs={"post_id": 42}))

Alternative Fix: Pass a Two-Tuple

If you are including URL patterns directly rather than pointing at a module string, you can provide the app name explicitly as part of a two-tuple.

python
1from django.urls import include, path
2from blog.urls import urlpatterns as blog_patterns
3
4urlpatterns = [
5    path("blog/", include((blog_patterns, "blog"), namespace="blog")),
6]

This satisfies the same requirement. Django now knows both the URL patterns and the associated app_name.

When You Do Not Need namespace=

If you are not actually using URL namespacing, remove the namespace argument entirely:

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

That avoids the error because Django no longer needs namespace metadata. Do this only if you genuinely do not need namespaced URL reversing.

Understanding app_name vs namespace

These two terms are related but not identical:

  • 'app_name identifies the application-level namespace,'
  • 'namespace identifies a particular included instance.'

In simple projects, they often have the same string value, which is why examples commonly use "blog" for both. But Django still treats them as separate concepts.

This distinction matters when you include the same app URLconf more than once under different instance namespaces.

python
1urlpatterns = [
2    path("authors/", include("blog.urls", namespace="authors_blog")),
3    path("editors/", include("blog.urls", namespace="editors_blog")),
4]

The shared app_name can still be "blog", while the instance namespaces differ.

A Full Minimal Example

Project urls.py:

python
1from django.urls import include, path
2
3urlpatterns = [
4    path("shop/", include("shop.urls", namespace="shop")),
5]

App shop/urls.py:

python
1from django.urls import path
2from . import views
3
4app_name = "shop"
5
6urlpatterns = [
7    path("", views.home, name="home"),
8]

Template usage:

django
<a href="{% url 'shop:home' %}">Shop</a>

With app_name in place, Django can resolve the namespaced URL correctly.

Common Pitfalls

The biggest pitfall is adding namespace="blog" in the project URLconf and forgetting to define app_name = "blog" in the included app URLconf.

Another mistake is thinking name= on individual path() entries replaces app_name. It does not. Route names and application namespaces solve different problems.

Developers also sometimes copy an example with include((patterns, "blog"), namespace="blog") without realizing that the tuple is the explicit source of app_name in that form.

Finally, if you are not reversing URLs with a namespace at all, remove the namespace instead of layering on unnecessary configuration.

Summary

  • The error occurs because namespace= in include() requires an app_name.
  • The simplest fix is to add app_name = "your_app" in the included urls.py.
  • If you include raw patterns, pass a two-tuple of (urlpatterns, app_name).
  • Remove namespace= entirely if you do not need namespaced URL reversing.
  • 'app_name and namespace are related, but they are not the same thing in Django's URL system.'

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.