未加星标

Disabling the Forms in Django Rest Framework's Browsable API

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

If you're building a RESTful api using django, then you're probably aware of Django Rest Framework . It's a great project that will do a lot of the heavy lifting for you. It's also got this really really nice featur: the browsable api .

The browsable api gives you out-of-the box access to view your api, and even to interact with it using some auto-generated forms. This is great during development, because you can quickly see exaclty how your api works. And the browsable api is also great for production, because it doubles as public documentation (provided you put some care and effort into your docstrings, but that's another post).

However , a lot of people seem to want to disable the browsable api's forms for their production site. I think this makes sense, and I'm one of those people! Here's how I made it happen:

DRF uses a class to render the browsable api, aptly named the BrowsableAPIRenderer . When it generates its context, it creates a display_edit_forms variable, and we need to override that. So, we'll create own own renderer class:

from rest_framework.renderers import BrowsableAPIRenderer
class BrowsableAPIRendererWithoutForms(BrowsableAPIRenderer):
"""Renders the browsable api, but excludes the forms."""
def get_context(self, *args, **kwargs):
ctx = super().get_context(*args, **kwargs)
ctx['display_edit_forms'] = False
return ctx

You can put that anywhere in your project. I typically have a utils app in most of my projects, so I put that in utils/renderers.py .

DRF uses a built-in setting to define a number of its renderes, so we need to override the DEFAULT_RENDERER_CLASSES . My settings for DRF now look something like this (including the setting for pagination):

REST_FRAMEWORK = {
'PAGINATE_BY': 100,
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'utils.api.BrowsableAPIRendererWithoutForms',
),
}

And there you have it. Go browse your django rest_framework-powered api, and notice that there are no forms!

Update: August 22, 2016

While the above worked for quite some time for me, it's certainly a sub-optimal solution, because the BrowsableAPIRendererWithoutForms class will still do all of the work to render the forms.

A slightly better solution to this problem is to short-circuit that process altogether. We can do that by overriding two of the parent class's methods:

from rest_framework.renderers import BrowsableAPIRenderer
class BrowsableAPIRendererWithoutForms(BrowsableAPIRenderer):
"""Renders the browsable api, but excludes the forms."""
def get_context(self, *args, **kwargs):
ctx = super().get_context(*args, **kwargs)
ctx['display_edit_forms'] = False
return ctx
def show_form_for_method(self, view, method, request, obj):
"""We never want to do this! So just return False."""
return False
def get_rendered_html_form(self, data, view, method, request):
"""Why render _any_ forms at all. This method should return
rendered HTML, so let's simply return an empty string.
"""
return ""

That's it! You shouldn't see any forms on your browseable api, and they should be just a small bit faster now, since we no longer do any form rendering work.

When doing this kind of stuff, It's always a good idea to look over the original source code, and you can do that here (DRF v. 3.4.4) . If you've stumbled across this post, first of all thanks for reading! Secondly, if you have any suggestions, please let me know in the comments below.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: DjangoRESTHTMLAUSSE
分页:12
转载请注明
本文标题:Disabling the Forms in Django Rest Framework's Browsable API
本站链接:http://www.codesec.net/view/483676.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(34)