In Django RestFramework, how to change the Api Root documentation?

后端 未结 6 1492
遇见更好的自我
遇见更好的自我 2021-02-04 02:12

In django RestFramework, is there any \"official\" way to generate the documentation for the \"Api Root\" ?

After looking at the RestFramework\'s source code, I\'ve foun

6条回答
  •  礼貌的吻别
    2021-02-04 03:07

    Thanks to frost-nzcr4's comment above, I found a nice way to do this:

    
    from rest_framework import routers
    from django.utils.safestring import mark_safe
    
    
    class MyAPIRootView(routers.APIRootView):
        """
        Controls appearance of the API root view
        """
    
        def get_view_name(self) -> str:
            return "My API"
    
        def get_view_description(self, html=False) -> str:
            text = "My REST API"
            if html:
                return mark_safe(f"

    {text}

    ") else: return text class MyRouter(routers.DefaultRouter): APIRootView = MyAPIRootView

    Then use this router in your urls.py:

    router = MyRouter()
    

提交回复
热议问题