[Django]-Reverse for 'api' not found. 'api' is not a valid view function or pattern name with rest-framework

6👍

Router provides argument basename which is using to reverse url.

router = routers.DefaultRouter()
router.register(r'genres', GenreViewSet, basename='genres')

urlpatterns = [
    url(r'^api/',include(router.urls)),
    path('', views.index, name='index'),

Note that DRF’s viewset has multiple urls. So you need to specify which one you want to use by adding specific suffix -list or -detail. First one will give you url of viewset list() and create() actions. And second using for retrieve() and update().

So in template it would be something like this:

<a href="{% url 'genres-list' %}">api</a>

Leave a comment