使用Django Rest Framework的APIView可浏览的API? [英] Using Django Rest Framework's browsable API with APIViews?

查看:172
本文介绍了使用Django Rest Framework的APIView可浏览的API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个视图:

class MyAPIView(APIView):
    def get(self, request, name=None):
        return {"hello": name or "world"}

如何获得包含在生成的文档中?具体来说,如何将其包含在API根中,因此当我访问 http://example.com/api/

How can I get that included in the generated documentation? Specifically, how can I get it included in the API Root, so it appears when I visit "http://example.com/api/"?

文档包含一个具有描述的APIView示例,但不描述实际将其包含在API浏览器中的过程。

The documentation includes an example of an APIView with description, but doesn't describe the process of actually getting it included in the API browser.

推荐答案


生成的文档?

the generated documentation?

不会将可浏览的API描述为生成的文档。

Hi David, first up I wouldn't quite describe the browsable API as 'generated documentation'.

如果您需要静态文档,则最好查看第三个派对工具,如 django-rest-swagger

If you need static documentation you're best off looking at a third party tool like django-rest-swagger.

可浏览的API意味着您构建的API将自我描述,但有一点不同从传统的静态文档工具。可浏览的API确保您在API中创建的所有端点都能够使用机器可读(即JSON)和人类可读(即HTML)表示进行响应。它还确保您可以通过浏览器直接完全交互 - 通常使用编程客户端进行交互的任何URL也可以通过浏览器友好的视图进行响应。

The browsable API does mean that the APIs you build will be self-describing, but it's a little different from conventional static documentation tools. The browsable API ensures that all the endpoints you create in your API are able to respond both with machine readable (ie JSON) and human readable (ie HTML) representations. It also ensures you can fully interact directly through the browser - any URL that you would normally interact with using a programmatic client will also be capable of responding with a browser friendly view onto the API.


如何获得包含。

How can I get that included.

只需添加一个docstring到视图,它将被包含在可浏览的API表示中,无论您通过哪个URL访问该视图。

Just add a docstring to the view and it'll be included in the browsable API representation of whichever URLs you route to that view.

默认情况下,您可以使用markdown在描述中包含HTML标记的符号,但您也可以自定义行为,例如,如果你宁愿使用第一个。

By default you can use markdown notation to include HTML markup in the description but you can also customise that behaviour, for example if you'd rather use rst.


具体来说,我如何得到它在API根中。

Specifically, how can I get it included in the API Root.

您只需要明确地将URL添加到您所连接的任何视图返回的响应中到 / api / 。例如...

You'll just want to explicitly add the URL to into the response returned by whatever view you have wired up to /api/. For example...

from rest_framework import renderers
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.reverse import reverse


class APIRoot(APIView):
    def get(self, request):
        # Assuming we have views named 'foo-view' and 'bar-view'
        # in our project's URLconf.
        return Response({
            'foo': reverse('foo-view', request=request),
            'bar': reverse('bar-view', request=request)
        })

这篇关于使用Django Rest Framework的APIView可浏览的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆