如果有基于 Class 的视图,为什么我必须使用基于函数的视图?

Why must I use Function-based views if there is Class-based?

来自 Django 文档

In the beginning there was only the view function contract, Django passed your function an HttpRequest and expected back an HttpResponse. This was the extent of what Django provided. Early on it was recognized that there were common idioms and patterns found in view development. Function-based generic views were introduced to abstract these patterns and ease view development for the common cases. The problem with function-based generic views is that while they covered the simple cases well, there was no way to extend or customize them beyond some configuration options, limiting their usefulness in many real-world applications. Class-based generic views were created with the same objective as function-based generic views, to make view development easier. However, the way the solution is implemented, through the use of mixins, provides a toolkit that results in class-based generic views being more extensible and flexible than their function-based counterparts.strong text

Class-based Django 中的视图非常好。它们非常抽象,可以自己处理很多事情。也可以更改它们以适应您想要实现的内容,但是 function-based 视图非常明确,如果您要实现一些不同的或复杂的东西,通常会更直接。 This may show you why some people like to use function-based views