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.
Routers are used with ViewSets in django rest framework to auto config the urls. Routers provides a simple, quick and consistent way of wiring ViewSet logic to a set of URLs. Router automatically maps the incoming request to proper viewset action based on the request method type(i.e GET, POST, etc).
The basename designates the start of the urls generated by the router.
A route can be defined as a URL that displays a particular web page on the browser. For example, if we want to visit the Educative website, we would head over to https://www.educative.io. Django lets us route URLs however we want and with no framework limitations.
URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It's a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With