41

I've been developing a REST backend with the Django REST Framework.
However, I'm having trouble adding a APIView instance to the web browsable API.

The documentation and the previous answer suggests that all I have to do is add a docstring.
It did not work for me.

I'm under the assumption that the browsable API only displays Viewset endpoints are registered with the router.
If this is so, how can I register APIView classes to the router?

Below is my current router code:

router = DefaultRouter(trailing_slash=False)
router.register(r'tokens', TokenViewSet, base_name='token')    
urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api/', include(router.urls)),
    url(r'^api/register$', RegisterUser.as_view(), name='register_user'),
    url(r'^api/auth$', ObtainAuthToken.as_view(), name='obtain_token'),
    url(r'^api/me$', ObtainProfile.as_view(), name='obtain_profile'),
    url(r'^api/recover$', FindUsername.as_view(), name='recover_username'),
)

Currently, only the Token endpoint shows up.

Thank you.

1
  • Can you confirm your running DRF3? What response code/body to you get when making regular browser requests (HTTP with a accept: text/html) for the /api/me and /api/recover endpoints (which I assume are APIViews)? Commented Apr 21, 2015 at 5:35

2 Answers 2

26

Routers aren't designed for normal views. You need use ViewSet if you want register you url to your router.

I have the same question here. Maybe you can ref it: How can I register a single view (not a viewset) on my router?

Sign up to request clarification or add additional context in comments.

Comments

4

I believe the line that includes the router.urls is 'preempting' other urls starting with api. Try changing,

url(r'^api/', include(router.urls)),

to

url(r'^tokenapi/', include(router.urls)),

If that works then try moving the line with include to be the last line in the url patterns list and changing tokenapi back to api.

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api/register$', RegisterUser.as_view(), name='register_user'),
    url(r'^api/auth$', ObtainAuthToken.as_view(), name='obtain_token'),
    url(r'^api/me$', ObtainProfile.as_view(), name='obtain_profile'),
    url(r'^api/recover$', FindUsername.as_view(), name='recover_username'),
    url(r'^api/', include(router.urls)),
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.