Problems with using include in Django

Asked

Viewed 1,130 times

0

I’m having trouble with the include function, according to the Django documentation, the syntax is include(module, namespace=None), include(pattern_list),include((pattern_list, app_namespace), namespace=None).


Source code

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
     path('admin/', admin.site.urls),
     path('clientes/', include("cliente.urls", namespace="cliente")),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Error

path('clientes/', include("cliente.urls", namespace="cliente")),
File "/home/phomint/DjangoProjects/Manager/myvenv/lib/python3.5/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.
  • Did you look at the documentation for the right version? The information in the question differs from the documentation in 1.11: https://docs.djangoproject.com/en/1.11/ref/urls/#include

  • I looked at my project and I’m using v2.1 and I looked at the documentation for it. @Andersoncarloswoss

  • You need to include app_name in the client app’s urls.py and not in the urls.py file you want to put include in. That’s why you keep giving the error.

2 answers

1

As you wrote, the syntax can be

include((pattern_list, app_namespace), namespace=None)

So in your case:

path('clientes/', include(("cliente.urls", "cliente"), namespace="cliente"))

0

In your file cliente.urls create a variable app_name='cliente'.

Example:

"""Arquivos urls.py que está em cliente"""
from django.urls import path
from . import views

app_name='cliente'

urlpatterns = [
    path('', views.index, name='index'),
]
  • Just create this variable does not change anything, I have to call there somewhere else ?

  • I simulated the error here. Dai when adding app_name='cliente' in the archive cliente/urls.py the error for. # app_name='cliente' the error is the same as what you posted. Basically only adding this variable should work. The only place I found reference to this error was at django-rest-framework, Following reference: Github of the Django-Rest-framework.

  • I found in the Django documentation the reference to app_name: https://docs.djangoproject.com/pt-br/2.1/topics/http/urls/#id5

  • So I read through the documentation and I tried it in its own way, but the error continues, with the include I am directing to the file of clients' urls and I need to declare the namespace to call it in the view. It works similar to the app_name but it is to give the name to the url file itself. @Renatocruz

Browser other questions tagged

You are not signed in. Login or sign up in order to post.