官术网_书友最值得收藏!

Class-based generic views

Class-based generic views are commonly used views implemented in an object-oriented manner (specifically the template method pattern) for better reuse. I hate the term generic views. I would rather call them stock views. Like stock photographs, you can use them for many common needs with a bit of tweaking.

Generic views were created because Django developers felt that they were recreating the same kind of views in every project. Nearly every project needed a page showing a list of objects (ListView), details of an object (DetailView), or a form to create an object (CreateView). In the spirit of DRY, these reusable views were bundled with Django.

A convenient table of generic views in Django 2.0 is given here:

We have not mentioned base classes such as BaseDetailView or mixins such as SingleObjectMixin here. They are designed to be parent classes. In most cases, you would not use them directly.

I strongly recommend you pick the most appropriate generic view. For example, instead of using a ListView you can implement the same view using a TemplateView or even a View. However, you will miss most of the benefits of using a generic view.

So, familiarize yourself with this table and find the generic view that strongly matches your requirement. The best reference for generic views is Classy Class-Based Views at http://ccbv.co.uk/ (most Django developers have memorized the URL). You will find all the attributes and methods of each view mentioned here.

主站蜘蛛池模板: 邹城市| 陕西省| 喀喇沁旗| 莱州市| 鄂托克旗| 郧西县| 合川市| 前郭尔| 舟曲县| 双鸭山市| 普安县| 集安市| 犍为县| 博兴县| 绩溪县| 梅河口市| 乌拉特后旗| 肥西县| 阿克陶县| 深圳市| 烟台市| 克拉玛依市| 澄江县| 沈丘县| 寿阳县| 永福县| 永新县| 延寿县| 奉节县| 尚志市| 奉贤区| 共和县| 龙口市| 永仁县| 青阳县| 冷水江市| 固安县| 肇庆市| 广州市| 独山县| 宝应县|