Django中的泛型,视图,视图集和Mixins有什么区别? [英] What are the differences between Generics, Views, Viewsets and Mixins in Django?

查看:132
本文介绍了Django中的泛型,视图,视图集和Mixins有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django和Django-Rest的新手。我对何时应使用它们感到困惑?它们的优缺点是什么?我只看过这个-http://www.cdrf.co

I am new to Django and Django-Rest. I am confused about when I should use these? what are their advantages and disadvantages? I have only seen this- http://www.cdrf.co

我唯一知道的是有很多方法可以做一件事情。

The only thing I know is there are a lot of ways to do 1 thing. But this is totally unclear to me.

推荐答案

在Django中,这四个术语在项目中经常用于不同目的。我试图通过指向每个术语的详细说明的链接来收集和共享实际含义。请检查是否对您有帮助。

In Django, these four terms we use frequently for different purposes in the projects. I have tried to collect and share the actual meaning with the links to details description of each term. Please check if you find these helpful.

通用视图:

Generic views:

Django的通用视图...被开发为通用用法模式的快捷方式...它们采用了视图开发中发现的某些通用习语和模式并将其抽象化,以便您可以快速编写通用数据视图而无需重复自己。
- Django文档

"Django’s generic views... were developed as a shortcut for common usage patterns... They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to repeat yourself." — Django Documentation

了解更多详情

观看次数:

Views:

视图函数或简称view是一个Python函数,它接受Web请求并返回Web响应。此响应可以是网页的HTML内容,重定向,404错误,XML文档或图像。 。 。或任何东西,真的。视图本身包含返回该响应所需的任何任意逻辑。这段代码可以在您想要的任何地方使用,只要它在Python路径上即可。可以说,没有其他要求,也没有魔术。为了将代码放置在某处,约定是将视图放置在名为``views.py''的文件中,该文件位于项目或应用程序目录中。

A view function, or view for short, is simply a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as long as it’s on your Python path. There’s no other requirement–no "magic," so to speak. For the sake of putting the code somewhere, the convention is to put views in a file called views.py, placed in your project or application directory.

了解更多详情

视图集:

Viewsets:

Django REST框架允许您在单个类中组合一组相关视图的逻辑,称为ViewSet。在其他框架中,您可能还会发现概念上类似的实现,它们的名称类似于资源或控制器。
ViewSet类只是基于类的View的一种,它不提供任何方法处理程序,例如.get()或.post(),而是提供诸如.list()和.create( )。

Django REST framework allows you to combine the logic for a set of related views in a single class, called a ViewSet. In other frameworks, you may also find conceptually similar implementations named something like 'Resources' or 'Controllers'. A ViewSet class is simply a type of class-based View, that does not provide any method handlers such as .get() or .post(), and instead provides actions such as .list() and .create().

ViewSet的方法处理程序仅在完成视图时才使用.as_view()方法绑定到相应的操作。

The method handlers for a ViewSet are only bound to the corresponding actions at the point of finalizing the view, using the .as_view() method.

了解更多详细信息

Mixins:

Mixins:

mixin类提供用于提供基本视图行为的操作。请注意,mixin类提供操作方法而不是直接定义处理程序方法,例如.get()和.post()。这样可以更灵活地构成行为。
mixin类可以从rest_framework.mixins导入。

The mixin classes provide the actions that are used to provide the basic view behavior. Note that the mixin classes provide action methods rather than defining the handler methods, such as .get() and .post(), directly. This allows for more flexible composition of behavior. The mixin classes can be imported from rest_framework.mixins.

了解更多详情

这篇关于Django中的泛型,视图,视图集和Mixins有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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