Update tech_docs/python/django.md
This commit is contained in:
@@ -1,3 +1,41 @@
|
||||
# Django Framework Structure
|
||||
|
||||
Django follows a structure often referred to as MVT (Model-View-Template), which is closely related to the more common MVC (Model-View-Controller) pattern. Here's a breakdown of each component:
|
||||
|
||||
## 1. Model (M)
|
||||
- Represents the data structure
|
||||
- Contains the essential fields and behaviors of the data you're storing
|
||||
- Usually maps to a single database table
|
||||
- Example: A `User` model might contain fields like `username`, `email`, `password`
|
||||
|
||||
## 2. View (V)
|
||||
- Receives web requests and returns web responses
|
||||
- Contains the logic to process the data from the models
|
||||
- Equivalent to the Controller in MVC frameworks
|
||||
- Example: A view might fetch all users from the database and prepare them for display
|
||||
|
||||
## 3. Template (T)
|
||||
- Describes how the data should be presented
|
||||
- Usually HTML with Django template language
|
||||
- Equivalent to the View in MVC frameworks
|
||||
- Example: An HTML file that loops through a list of users and displays their information
|
||||
|
||||
## Key Points:
|
||||
- Django's "View" is not the same as MVC's "View"
|
||||
- Django's "Template" serves the purpose of MVC's "View"
|
||||
- The framework provides a URL dispatcher (urls.py) that maps URLs to views
|
||||
|
||||
## Flow of a typical Django request:
|
||||
1. A request comes to a URL
|
||||
2. Django uses the URL configuration to call the appropriate View
|
||||
3. The View interacts with the Model to fetch/update data
|
||||
4. The View then renders a Template, passing it any necessary data
|
||||
5. The rendered Template is sent back as a response to the user
|
||||
|
||||
This structure allows for a clear separation of concerns, making Django applications easier to develop, maintain, and scale.
|
||||
|
||||
---
|
||||
|
||||
[django training](https://youtu.be/Rp5vd34d-z4?si=F8GugiXId9h6E9rK&t=1877)
|
||||
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user