Django Features & Architecture (MVT)

beginner
14 min

Django Features & Architecture (MVT)

Welcome to our comprehensive Django tutorial! In this lesson, we'll delve into Django's unique features, its Model-View-Template (MVT) architecture, and how to build a real-world project. Let's get started!

What is Django? 🎯

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It's built by experienced developers and takes the best of other frameworks to create a powerful and flexible tool.

Why Django? πŸ’‘

Django offers several advantages such as:

  1. Batteries Included: Django comes with many components baked in, reducing the need for third-party libraries.
  2. The DRY (Don't Repeat Yourself) Principle: Django emphasizes code reusability to make development easier and faster.
  3. Security: Django includes several security features out-of-the-box, reducing the risk of common web vulnerabilities.

Django's MVT Architecture πŸ“

Django's MVT architecture is a way to structure web applications. It separates the application logic into three main components:

  1. Model: Represents the data, like database tables.
  2. View: Handles the request and produces the response.
  3. Template: Contains the presentation logic and generates HTML.

Creating a Simple Django Project 🎯

Let's create a simple project and see how these components work together.

Step 1: Install Django

First, you need to install Django using pip:

bash
pip install django

Step 2: Create a New Project

Create a new Django project:

bash
django-admin startproject my_project

Step 3: Create an App

Now, let's create a new app:

bash
cd my_project python manage.py startapp my_app

Step 4: Define a Model

Open my_app/models.py and define a simple model:

python
from django.db import models class Book(models.Model): title = models.CharField(max_length=100) author = models.CharField(max_length=100) publication_date = models.DateField()

Step 5: Migrate the Database

Run the following command to apply the database migration:

bash
python manage.py makemigrations my_app python manage.py migrate

Step 6: Create a View

Open my_app/views.py and create a simple view:

python
from django.http import HttpResponse from my_app.models import Book def book_list(request): books = Book.objects.all() output = '<ul>' for book in books: output += f'<li>{book.title}, {book.author}, {book.publication_date}</li>' output += '</ul>' return HttpResponse(output)

Step 7: Define a URL

Open my_app/urls.py and define a URL for the view:

python
from django.urls import path from . import views urlpatterns = [ path('books/', views.book_list, name='book_list'), ]

Step 8: Include the App in Project's URLs

Open my_project/urls.py and include the app's URLs:

python
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('my_app.urls')), ]

Step 9: Run the Server

Start the development server:

bash
python manage.py runserver

Now, if you navigate to http://127.0.0.1:8000/books/, you'll see a list of books generated by our simple view!

Wrapping Up πŸ“

We've covered the basics of Django, its MVT architecture, and how to create a simple project. In the next lessons, we'll delve deeper into Django, exploring templates, forms, and more advanced topics.

Quiz

Quick Quiz
Question 1 of 1

What is the purpose of Django's MVT architecture?