Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Final Details url Tag

Flore W
Flore W
4,744 Points

Namespace error with Django 2.0

Hi all, I can't make namespace work with Django 2.0. I checked on the documentation, and it says that you have to define the app_name. But I don't know which app_name to give and also in which file... can someone help me please? Everything else until then works as in the video.

That's my learning_site/urls.py file

from django.contrib import admin
from django.urls import include, path
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from . import views

app_name = 'learning_site'

urlpatterns = [
    path('courses/', include('courses.urls', namespace='courses')),
    path('admin/', admin.site.urls),
    path('', views.hello_world, name='home'),
]

urlpatterns += staticfiles_urlpatterns()

And that's for example my course_list.html file

{% extends "layout.html" %}

{% block title %}Available Courses{% endblock %}

{% block content %}
<div class="cards">
    {% for course in courses %}
    <div class="card">
        <header><a href="{% url 'courses:detail' pk=course.pk %}">{{ course.title }}</a></header>
        <div class="card-copy">
            {{ course.description }}
        </div>
    </div>
    {% endfor %}
</div>
{% endblock %}

I get the error: "Django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead."

2 Answers

I had the same issue, but it seems that in Django 2 you need to add a name to your app in order to use namespacing.

I edited my courses/urls.py folder to include an app_name attribute of "courses" and the server stopped complaining.

See:

from django.urls import path
from django.conf.urls import include, url

from . import views

app_name = "courses"

urlpatterns = [
    path('', views.course_list, name='list'),
    path('<int:course_pk>/<int:step_pk>/', views.step_detail,
        name='step_detail'),
    path('<int:pk>/', views.course_detail, name='course_detail'),
]

I don't know if that app_name attribute has to match the namespacing or if there are any other requirements, but this worked for me for this project. Hope it helps.

Cheers, Owen

Flore W
Flore W
4,744 Points

Thanks Owen, this solved the issue! Thinking back it does make sense to give a name to that courses/urls.py file, so that whenever anything from that file is referenced inside the html files, we can just call it by [namespace]:[name].

Thanks a lot, Owen this is really helpful.