Reputation: 73
In django2.1.5 and DRF 3.9.1, I am trying to add router.urls namespace which doesn't work.
path('api/v2/', include(router.urls, namespace="v2"))
The error in my terminal is
"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"
I won't find any suitable solution as I set app_name for a namespace. How can I use the namespace on router.urls or there is no way to use it in Django 2 version?
Trying to add app_name but it won't solve my problem
Here is my code.
config.urls.py
from django.urls import path, include
from django.contrib import admin
from rest_framework import routers
from project.courses import views
router = routers.SimpleRouter()
router.register(r'courses', views.CourseViewSet)
router.register(r'reviews', views.ReviewViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')),
path('api/v1/courses/', include('project.courses.urls', namespace='courses')),
path('api/v2/', include(router.urls, namespace="v2")),
]
courses.urls.py
from django.urls import path
from . import views
app_name = 'project.courses'
urlpatterns = [
path('', views.ListCreateCourse.as_view(), name='course_list'),
path('<int:pk>', views.RetrieveUpdateDestroyCourse.as_view(),
name='course_detail'), path('/reviews/', views.ListCreateReview.as_view(), name='review_list'), path('/reviews/', views.RetrieveUpdateDestroyReview.as_view(), name='review_detail'), ]
Here is the code I want to write.
reviews = serializers.HyperlinkedRelatedField(
many=True,
read_only=True,
view_name='v2:review-detail'
)
I want to access review_detail with namespace v2. Thanks.
Upvotes: 7
Views: 3679
Reputation: 1397
You try like this,
urlpatterns = [
url(r'^api/', include((router.urls, 'app_name'), namespace='instance_name')),
]
Inside the include you can't add namespace. Follow the above way. If you any doubt refer this https://www.django-rest-framework.org/api-guide/routers/
Upvotes: 10