Table of contents
No headings in the article.
Here are some of the most common design patterns used in Django:
Model-View-Controller (MVC) - Django follows the model-view-template (MVT), which is a variation of the classic MVC pattern. The model represents the data, the view handles the presentation layer, and the template provides an interface for views and defines how data is presented.
Object-Relational Mapping (ORM) - Django uses its own ORM to interact with the database instead of writing raw SQL queries. It maps relational database tables to Python classes and objects, simplifying database interactions and making the code more Pythonic.
Template Method pattern - This pattern is used in django's
SubclassingView
or Class-based views (CBVs) to provide a framework for developers to override methods that perform a specific task. For example, thedispatch
method is the top-level method that is called when a view is requested. Django's CBVs definedispatch
as the final version of this method, but developers can override other CBV methods to customize the functionality.Decorator pattern - In Django, decorators are functions that modify the behavior of other functions. They're used to implement cross-cutting concerns such as authentication, authorization, caching, and more. Decorators can be applied to a view function or an entire view class.
Façade pattern - In Django, the facades are provided by the
django.contrib
package. These are high-level interfaces that simplify the use of complex modules, such as django.contrib.auth and SessionMiddleware, by providing a simplified interface.Singleton pattern - Django's settings module can be considered a singleton since it contains global configuration options used throughout the application.
Command pattern - Django's management commands are examples of the command pattern. These are custom management scripts that can be run from the command line to perform various tasks, such as running a development server, creating database tables, or deploying the application.
Overall, the architecture of Django follows a few important design principles, such as DRY (Don't Repeat Yourself) and the KISS (Keep It Simple, Stupid) principle. The framework encourages developers to write clean, reusable, and maintainable code.