Skip to content

0xNullLight/webdev-notes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Note:

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.


Understanding MVC, MVT, and Decoupled/Headless Architecture in Django

Model-View-Controller (Common Traditional Software Architecture Pattern)

Model-View-Controller (MVC)

1. Model-View-Controller (MVC)

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.

How MVC Works

  1. A user interacts with the View (e.g., clicks a button).
  2. The Controller handles this interaction, processes the input, and communicates with the Model.
  3. The Model performs the required business logic and updates data.
  4. The updated data is passed back to the View, which refreshes the display for the user.

Django's Relationship with MVC

  • 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.

Model-View-Template (Django's Software Architecture Pattern)

Model-View-Template (MVT)

2. Model-View-Template (MVT)

The MVT pattern is Django’s adaptation of MVC. It emphasizes server-side rendering, where templates are used to generate the user interface dynamically.

Key Components in MVT

  1. Model: Manages database interactions and encapsulates business logic.
    • Example: A Product model might define fields like name, price, and description.
  2. 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.
  3. Template: Dynamically generates HTML using data provided by the view.
    • Example: product_list.html displays a list of products.

Workflow in Django MVT

  1. A user sends an HTTP request (e.g., GET /products/).
  2. The View fetches data from the Model.
  3. The data is passed to the Template to render HTML.
  4. The server sends the rendered HTML back to the user.

Example

# 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 %}

3. Decoupled/Headless Architecture

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).

Key Components in Headless Architecture

  1. Back-End (Django):
    • Manages database interactions and business logic.
    • Exposes data through APIs (e.g., Django REST Framework or GraphQL).
  2. Front-End (Client):
    • Consumes APIs to fetch and display data.
    • Handles routing, user interactions, and UI updates.

Workflow in Decoupled Architecture

  1. A user interacts with the front-end app (e.g., clicking a button in React).
  2. The front-end sends an API request (e.g., GET /api/products/) to the Django back-end.
  3. Django processes the request and returns JSON data.
  4. The front-end updates the UI based on the data.

Example with Django REST Framework and React

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;

Comparison of MVT and Decoupled Architecture

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).

Conclusion

  • 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published