Python 将 APIView 添加到 Django REST Framework 可浏览 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28730109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:39:57  来源:igfitidea点击:

Adding a APIView to Django REST Framework Browsable API

pythondjangorestdjango-rest-framework

提问by Jae

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.

我一直在使用 Django REST 框架开发 REST 后端。
但是,我无法将 APIView 实例添加到 Web 可浏览 API。

The documentationand the previous answersuggests 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?

我假设可浏览的 API 仅显示已向路由器注册的 Viewset 端点。
如果是这样,我如何将 APIView 类注册到路由器?

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.

目前,仅显示 Token 端点。

Thank you.

谢谢你。

回答by nmgeek

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

我相信包含 router.urls 的行是“抢占”以api开头的其他 url 。尝试改变,

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

to

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

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

如果可行,则尝试将包含 include的行移动到 url 模式列表中的最后一行,并将tokenapi更改回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)),
)

回答by flytofuture

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

路由器不是为普通视图设计的。如果要将 url 注册到路由器,则需要使用 ViewSet。

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

我在这里也有同样的问题。也许您可以参考它: 如何在路由器上注册单个视图(而不是视图集)?