Search code examples
djangodjango-rest-frameworkdjango-viewsdjango-urlsdjango-rest-framework-simplejwt

Why I cannot register '.as_view()' in the Django router (using djangorestframework-simplejwt)?


I have been trying to add djangorestframework-simplejwt to my webapp and whenever I use the router to add the views (see the 2 commented routers in the code below) - I get an error (see traceback).

When adding the urls as the documentation says - under the urlpatterns variable - it works without any errors. I would like to know why this isn't working through the router.register(..) and for the sake of good order (and my OCD), is it possible to move it out of the urlpatterns variable to the router.

from django.urls import path, include
from rest_framework import routers
from . import views

from django.conf.urls import url
from django.contrib import admin
from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)

app_name = 'myapi'

router = routers.DefaultRouter()
router.register(r'posts', views.PostViewSet)
router.register(r'user', views.UserViewSet)
router.register(r'likes', views.LikesViewSet)
# router.register(r'token',TokenObtainPairView.as_view())
# router.register(r'token/refresh',jwt_views.TokenRefreshView.as_view())

urlpatterns = [
    path('', include(router.urls)), 
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]

traceback:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 118, in inner_run
    self.check(display_num_errors=True)
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/core/management/base.py", line 392, in check
    all_issues = checks.run_checks(
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/core/checks/registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/urls/resolvers.py", line 408, in check
    for pattern in self.url_patterns:
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/urls/resolvers.py", line 589, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
    return import_module(self.urlconf_name)
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/test/Projects/ppproject/src/ufb/urls.py", line 25, in <module>
    path('api/', include('myapi.urls', namespace='myapi')),
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/test/Projects/ppproject/src/myapi/urls.py", line 18, in <module>
    router.register(r'token',TokenObtainPairView.as_view())
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/rest_framework/routers.py", line 54, in register
    basename = self.get_default_basename(viewset)
  File "/home/test/anaconda3/envs/ppproject/lib/python3.9/site-packages/rest_framework/routers.py", line 137, in get_default_basename
    assert queryset is not None, '`basename` argument not specified, and could ' \
AssertionError: `basename` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset` attribute.

Solution

  • Routers work with ViewSets and not with API Views, so you can't add them at that level.

    In Django land, every url maps to a view function, and it is the Router's job to convert that ViewSet to a list of functions that django's path knows how to handle, by routing GET to the list() function, etc. So router.urls is a list of django path instances.

    You can even do it manually if you really like to confuse others!

    snippet_list = SnippetViewSet.as_view({
        'get': 'list',
        'post': 'create'
    })
    
    path('snippets/', snippet_list, name='snippet-list'),
    

    The way you are adding them now is correct. as_view is a DRF function which converts an APIView or related class into a view function for django's path to work with.

    I have a route() helper which you might find helpful. It unifies routing for views, '.urls', viewsets, and replaces path(). I found this was one of my biggest frustrations over the years.

    urlpatterns = [
        route('students/', 'student.urls'),
        route('users/', UserViewSet, name='user'),
        route('register/', RegisterView, name='register'),
        ...