- Django Design Patterns and Best Practices
- Arun Ravindran
- 243字
- 2021-06-25 21:32:11
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.
- 大學計算機基礎實驗教程
- Instant QlikView 11 Application Development
- Java Web應用開發技術與案例教程(第2版)
- Learning Concurrency in Kotlin
- Getting Started with React Native
- Kubernetes源碼剖析
- JBoss:Developer's Guide
- 零基礎學Scratch 3.0編程
- CodeIgniter Web Application Blueprints
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- SQL Server 2012 數據庫應用教程(第3版)
- Raspberry Pi Blueprints
- Learning Gerrit Code Review
- Lync Server Cookbook
- Instant OpenCV for iOS