Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Django Introduction

Django Introduction

Django is an open-source web application framework written in Python.

With Django, with very little code, Python developers can easily accomplish most of what a formal website needs and further develop full-featured web services.

Django provides the tools needed for full-stack development, including database ORM, template engine, routing system, user authentication, etc., greatly reducing repetitive code.

Django's philosophy:

Django follows the MVC (Model-View-Controller) architecture, but in Django it is more commonly called MTV (Model-Template-View).

Built-in features

Function Description
Admin backend Automatically generates the management interface, no need to manually write CRUD logic.
ORM Operate the database with Python classes, no need to write SQL.
Form handling Built-in form validation to prevent CSRF attacks.
User Authentication Provides login, registration, and permission management (django.contrib.auth)。
Routing System Flexible URL mapping, supporting regular expressions.
Caching mechanism Supports backends such as Memcached and Redis.

Core features


MVC and MTV models

MVC (Model-View-Controller)

Workflow:

  1. The user sends a request to the Controller.

  2. The Controller processes the logic and calls the Model to get data.

  3. The Controller passes the data to the View.

  4. The View renders and returns the HTML page to the user.

MVT (Model-Template-View) -- Django's implementation approach

Django adopts the MVT design pattern, similar to MVC but with some differences:

Workflow:

  1. The user accesses a URL, and the request is mapped by Django's urls.py to the corresponding View.

  2. The View processes business logic and calls the Model to get data.

  3. The View passes the data to the Template.

  4. The Template renders HTML, which is finally returned to the user.


Applicable scenarios for Django

Projects suitable for Django:

Unsuitable scenarios:


Django vs other frameworks

Framework Feature
Flask Lightweight and flexible, suitable for small projects or microservices.
FastAPI Strong asynchronous support, suitable for high-performance API development.
Ruby on Rails Similar to Django, but uses the Ruby language.
Laravel An MVC framework in the PHP ecosystem, with functionality similar to Django.

** Django advantages:**