Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register an @api_view into router, and i can see it in API root page?

This my test code,but it is not work:

from django.conf.urls import url, include
from rest_framework import routers
from apps.apis import views


router = routers.DefaultRouter()
router.register(r'users1', views.UserViewSet)
router.register(r'hello', views.hello, base_name='hello')

urlpatterns = [
    url(r'', include(router.urls)),
    url(r'^hello$', views.hello ),
    url(r'^api-auth/', include('rest_framework.urls',namespace='rest_framework')),
]

How to register an @api_view into router, and i can see it in API root page?Thanks all.

like image 269
pyli.xm Avatar asked Oct 31 '25 03:10

pyli.xm


1 Answers

You can't.

Your view needs to inherit from ViewSet to appear in the routers. This being said, you can simply turn an APIView into a ViewSet by adding the ViewSetMixin

like image 84
Linovia Avatar answered Nov 03 '25 00:11

Linovia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!