157

I am currently trying out Django. I use the namespace argument in one of my include()s in urls.py. When I run the server and try to browse, I get this error.

File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\conf.py", line 39, in include
    'Specifying a namespace in include() without providing an app_name '
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.

These are my urls.py files:

#project/urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^reviews/', include('reviews.urls', namespace='reviews')),
    url(r'^admin/', include(admin.site.urls)),
]

and

#app/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    # ex: /
    url(r'^$', views.review_list, name='review_list'),
    # ex: /review/5/
    url(r'^review/(?P<review_id>[0-9]+)/$', views.review_detail, name='review_detail'),
    # ex: /wine/
    url(r'^wine$', views.wine_list, name='wine_list'),
    # ex: /wine/5/
    url(r'^wine/(?P<wine_id>[0-9]+)/$', views.wine_detail, name='wine_detail'),
]

What do I pass the app_name as stated in the error message?

coffee-grinder
  • 26,940
  • 19
  • 56
  • 82
Nelson M
  • 1,658
  • 4
  • 13
  • 19

7 Answers7

209

Check the docs for include here.

What you've done is not an acceptable way of passing parameters to include. You could do:

url(r'^reviews/', include(('reviews.urls', 'reviews'), namespace='reviews')),
Majid
  • 1,673
  • 18
  • 27
unixia
  • 4,102
  • 1
  • 19
  • 23
  • Just tried this out and got another error...url(r'^reviews/', include('reviews.urls', namespace='reviews', app_name='reviews')), TypeError: include() got an unexpected keyword argument 'app_name' – Nelson M Feb 04 '18 at 14:44
  • 1
    Extremely sorry. That was supposed to be used with modules not patterns. Please check the updated answer. – unixia Feb 04 '18 at 15:03
  • 1
    This solution was helpful for me as well. I was following the tutorial from https://django-oauth-toolkit.readthedocs.io/en/latest/tutorial/tutorial_02.html and there is an instruction to do: urlpatterns = [ # OAuth 2 endpoints: url(r'^o/', include(oauth2_endpoint_views, namespace="oauth2_provider")), url(r'^api/hello', ApiEndpoint.as_view()), # an example resource endpoint ]. This generates the same error as you mentioned. – Koji D'infinte May 11 '18 at 16:40
  • 1
    The tuple-syntax did not work for me, it only worked when I changed (my version of) `reviews.url` to have an `app_name` variable in `django 2.1`. – Herbert Nov 14 '18 at 14:27
  • What is the use case for this? I have a lot of included urls in my project and all with the same pattern: `path("the_app/", include(("app.urls", ), namespace=))` and it's quite annoying. – user4052054 Jun 27 '19 at 16:20
125

Django 1.11+, 2.0+

You should set the app_name in the urls file you are including

# reviews/urls.py  <-- i.e. in your app's urls.py

app_name = 'reviews'
     

Then you can include it the way you are doing it.

Also, it might be worth noting what Django docs say here https://docs.djangoproject.com/en/1.11/ref/urls/#include :

Deprecated since version 1.9: Support for the app_name argument is deprecated and will be removed in Django 2.0. Specify the app_name as explained in URL namespaces and included URLconfs instead.

( https://docs.djangoproject.com/en/1.11/topics/http/urls/#namespaces-and-include )

Bob
  • 5,809
  • 5
  • 36
  • 53
41

Django 2.0 you should specify app_name in your urls.py, is not necessary to specify app_name argument on include.

Main Url file.

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

urlpatterns = [
    path('', include('apps.main.urls')),
    path('admin/', admin.site.urls),
]

Included Url.

from django.urls import path
from . import views

app_name = 'main_app'

urlpatterns = [
    path('', views.index, name='index'),
]

Then use use in template as

<a href="{% url main_app:index' %}"> link </a>

More details: https://code.djangoproject.com/ticket/28691 Django 2.0 Docs

Matthew Hegarty
  • 3,791
  • 2
  • 27
  • 42
Brayan Loayza
  • 678
  • 7
  • 15
4

I included a library not (fully) django 2.1 compatible yet (django_auth_pro_saml2). Hence I create a second file saml_urls.py:

from django_saml2_pro_auth.urls import urlpatterns

app_name = 'saml'

Such that I could include the urls as:

from django.urls import include, re_path as url

urlpatterns = [
    ..., url(r'', include('your_app.saml_urls', namespace='saml')), ...
]

Hacky, but it worked for me, whereas the url(r'^reviews/', include(('reviews.urls', 'reviews'), namespace='reviews')) did not.

Herbert
  • 5,279
  • 5
  • 44
  • 69
2

I am also face the same error in Django 2.2 and i solve it this way

urls.py file

urlpatterns = [
   path('publisher-polls/', include('polls.urls', namespace='publisher-polls')),
]

polls/urls.py file

app_name = 'polls'
urlpatterns = [
  path('', views.IndexView.as_view(), name='index')
]

example use of namespace in calss based view method

def get_absolute_url(self):
    from django.urls import reverse
    return reverse('polls.index', args=[str(self.id)])

example use of namespace in templates

{% url 'polls:index' %}

Here polls:index mean app_name[define in polls/urls.py file]:name[define in polls/urls.py file inside path function]

their official which is pretty good you can check for more info namespace_django_official_doc

sayalok
  • 882
  • 3
  • 15
  • 30
0

Find unused files or directories in APP (reviews) directory, and detete this items !

0

In project level urls.py

path('',include('appname.urls'), namespace= 'test')

Now in in your app level urls.py

app_name = 'test'
path('',views.index, name= 'home')

Long story short is, You have to provide "app_name" in your app level urls and that same name should be your namespace name at project level url

Yashraj
  • 73
  • 1
  • 5