其余的Django框架api_view VS普通视图 [英] Django rest framework api_view vs normal view

查看:1528
本文介绍了其余的Django框架api_view VS普通视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在到处找找到这个体面的解释,他们都来了短... :当你使用@api_view装饰,而不是基于类视图用的django休息框架应用

I have been looking everywhere to find a decent explanation for this, and they all come short...When do you use the @api_view decorator rather than a class based view with the django rest framework app

推荐答案

REST框架之外,它是何时使用基于类与一般功能为本次欣赏到同样的问题。在Django CBVS是真棒,灵活和节省样板code的负荷,但有时它只是更快,更容易和更清晰的使用功能基础观点。想想看,你会采取编写在Django一个普通视图相同的方法。 REST框架简单地支持写入视图code这两种方法,因为它在<一介绍href=\"http://www.django-rest-framework.org/tutorial/2-requests-and-responses#wrapping-api-views\">tutorial.

REST Framework aside, it's the same question of when to use class based views versus function based views in general. CBVs in Django are awesome, flexible and save loads of boilerplate code, but sometimes it's just faster, easier and clearer to use a function based view. Think about it with the same approach you'd take to writing a normal view in Django. REST Framework simply supports both methods of writing view code as it introduces in the tutorial.

一般用CBV去,除非它在你的方式,然后保持简单与函数基于视图和装饰。在这两个Django和REST的框架,像列表,分页和CRUD操作的典型事物的逻辑已经写入和类和混入的形式易于扩展。如果你的观点的逻辑是做一些明显不同,功能基础观点可能是适当的。当然你可以在你的应用程序中使用这两种方法。

Generally go with a CBV unless it's getting in your way, then keep it simple with a function based view and the decorator. In both Django and the REST Framework, the logic for typical things like lists, pagination and CRUD operations is already written and easily extendable in the form of classes and mixins. If your view logic is doing something notably different, a function based view might be appropriate. And of course you can use both approaches in your app.

这篇关于其余的Django框架api_view VS普通视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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