Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define APIView's url?

I'm trying to make an APIView with the Django Rest Framework. When I associate the view with the url, I got this error :

AssertionError: basename argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset attribute.

Here is mys APIView :

class ExampleView(APIView):
    authentication_classes = (SessionAuthentication, BasicAuthentication)
    permission_classes = (IsAuthenticated,)

    def get(self, request, format=None):
        content = {
            'user': unicode(request.user),  # `django.contrib.auth.User` instance.
            'auth': unicode(request.auth),  # None
        }
        return Response(content)

And the router :

router = routers.DefaultRouter()
router.register('api/example', views.ExampleView.as_view())

So, what's wrong ? Thank you !

like image 437
Pythorogus Avatar asked Jan 21 '19 21:01

Pythorogus


1 Answers

You just need to add the path to your urlpatterns. Routers are used with viewsets.

from django.urls import path

app_name = 'example'

urlpatterns = [
    path('api/example', views.ExampleView.as_view(), name='example')
]
like image 79
Ken4scholars Avatar answered Nov 01 '22 21:11

Ken4scholars