As I was learning Django, I notice that utilizing the django shell is great!!! I think anyone whom is learning should know how to utilize
python3 manage.py shell
It's like using ipython, but is specifically with django on mind so you can just import certain libraries and utilize dir()
or help()
effectively with django objects to help you navigate.
The ability to learn how to navigate is key when it comes to python or learning language or framework.
The MVC pattern separates application logic into three interconnected components:
- Model: Represents the data and business logic of the application.
- View: Displays data to the user and receives input.
- Controller: Acts as a mediator between the Model and View, processing user input and updating the Model or View accordingly.
- A user interacts with the View (e.g., clicks a button).
- The Controller handles this interaction, processes the input, and communicates with the Model.
- The Model performs the required business logic and updates data.
- The updated data is passed back to the View, which refreshes the display for the user.
- Django loosely follows the MVC architecture, but it refers to its pattern as Model-View-Template (MVT).
- In Django:
- The Model represents the database and business logic (
models.py
). - The View handles requests and responses (
views.py
). - The Template (instead of a Controller) renders data into HTML for the user.
- The Model represents the database and business logic (
The MVT pattern is Django’s adaptation of MVC. It emphasizes server-side rendering, where templates are used to generate the user interface dynamically.
- Model: Manages database interactions and encapsulates business logic.
- Example: A
Product
model might define fields likename
,price
, anddescription
.
- Example: A
- View: Handles HTTP requests, processes data, and determines what to display.
- Example: A function in
views.py
fetches products and passes them to a template.
- Example: A function in
- Template: Dynamically generates HTML using data provided by the view.
- Example:
product_list.html
displays a list of products.
- Example:
- A user sends an HTTP request (e.g.,
GET /products/
). - The View fetches data from the Model.
- The data is passed to the Template to render HTML.
- The server sends the rendered HTML back to the user.
# models.py
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
# views.py
from django.shortcuts import render
from .models import Product
def product_list(request):
products = Product.objects.all()
return render(request, 'product_list.html', {'products': products})
# product_list.html
{% for product in products %}
<p>{{ product.name }} - ${{ product.price }}</p>
{% endfor %}
In a decoupled or headless architecture, Django serves purely as a back-end API provider, while a separate front-end application (e.g., React, Angular, or Vue) consumes these APIs to render the user interface. This approach is ideal for multi-platform applications (e.g., web, mobile, IoT).
- Back-End (Django):
- Manages database interactions and business logic.
- Exposes data through APIs (e.g., Django REST Framework or GraphQL).
- Front-End (Client):
- Consumes APIs to fetch and display data.
- Handles routing, user interactions, and UI updates.
- A user interacts with the front-end app (e.g., clicking a button in React).
- The front-end sends an API request (e.g.,
GET /api/products/
) to the Django back-end. - Django processes the request and returns JSON data.
- The front-end updates the UI based on the data.
Django Back-End
# models.py
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
# serializers.py
from rest_framework import serializers
from .models import Product
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'
# views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Product
from .serializers import ProductSerializer
class ProductListAPIView(APIView):
def get(self, request):
products = Product.objects.all()
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)
React Front-End
import React, { useEffect, useState } from 'react';
import axios from 'axios';
const ProductList = () => {
const [products, setProducts] = useState([]);
useEffect(() => {
axios.get('http://localhost:8000/api/products/')
.then(response => setProducts(response.data))
.catch(error => console.error(error));
}, []);
return (
<div>
{products.map(product => (
<p key={product.id}>{product.name} - ${product.price}</p>
))}
</div>
);
};
export default ProductList;
Aspect | MVT (Django with DTL) | Decoupled/Headless |
---|---|---|
Rendering | Server-side using Django templates. | Client-side using JavaScript frameworks. |
Routing | Managed by Django. | Managed by the front-end framework. |
API Usage | Optional, limited to AJAX or small integrations. | Core to the architecture; API-first approach. |
Development Workflow | Single workflow for both front-end and back-end. | Separate workflows for front-end and back-end teams. |
Flexibility | Tightly coupled to Django's structure. | Allows multiple clients (e.g., web, mobile). |
- MVC: Provides a foundational understanding of separating logic into layers.
- MVT: Django’s server-side rendering approach, ideal for simpler or tightly coupled applications.
- Decoupled/Headless: Perfect for modern, API-driven applications requiring flexibility, scalability, and multi-platform support.