Django MVT Architecture 🎯

beginner
24 min

Django MVT Architecture 🎯

Welcome to our deep dive into the Django MVT (Model-View-Template) Architecture! This tutorial is designed for beginners and intermediates, so let's get started! πŸš€

Understanding Django MVT Architecture πŸ“

In Django, the MVT architecture separates an application into three interconnected components:

  1. Model: It represents the data structure and handles data persistence.
  2. View: It handles user requests and generates dynamic content.
  3. Template: It defines the layout and structure of the dynamic content.

Models πŸ’‘

A Model in Django is a Python class that represents a database table. It defines the fields, data types, and relationships between different tables.

python
from django.db import models class BlogPost(models.Model): title = models.CharField(max_length=200) content = models.TextField() pub_date = models.DateTimeField('date published')

In this example, we have created a BlogPost model with three fields: title, content, and pub_date.

Views πŸ’‘

A View in Django is a Python function that handles HTTP requests and returns an HTTP response. It receives the request, processes it, and generates the appropriate response based on the request type.

python
from django.shortcuts import render from .models import BlogPost def blog_post_detail(request, pk): post = BlogPost.objects.get(id=pk) return render(request, 'blog_post_detail.html', {'post': post})

In this example, we have created a view function named blog_post_detail that takes a request and the primary key of a blog post as arguments. It retrieves the blog post from the database and passes it to a template called blog_post_detail.html.

Templates πŸ’‘

A Template in Django is an HTML file with placeholders for dynamic content. It defines the layout and structure of the dynamic content generated by the views.

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}Blog Post Detail{% endblock %}</title> </head> <body> {% block content %} <h1>{{ post.title }}</h1> <p>{{ post.content }}</p> {% endblock %} </body> </html>

In this example, we have a template with two placeholders: title and content. These placeholders will be replaced with the actual title and content of the blog post when the view renders the template.

Quiz Time 🎯

Quick Quiz
Question 1 of 1

What are the three components of Django's MVT architecture?

Practical Example 🎯

Let's create a simple blog application with a list of blog posts and a detail view for each post.

Setting Up the Project πŸ’‘

First, create a new Django project:

bash
django-admin startproject myblog cd myblog

Then, create a new app for our blog:

bash
python manage.py startapp blog

Creating the BlogPost Model πŸ’‘

Open the blog/models.py file and create the BlogPost model:

python
from django.db import models class BlogPost(models.Model): title = models.CharField(max_length=200) content = models.TextField() pub_date = models.DateTimeField('date published') def __str__(self): return self.title

Creating the BlogPostList and BlogPostDetail Views πŸ’‘

Open the blog/views.py file and create the BlogPostList and BlogPostDetail views:

python
from django.shortcuts import render, get_object_or_404 from .models import BlogPost def blog_post_list(request): posts = BlogPost.objects.all() return render(request, 'blog_post_list.html', {'posts': posts}) def blog_post_detail(request, pk): post = get_object_or_404(BlogPost, id=pk) return render(request, 'blog_post_detail.html', {'post': post})

Creating the BlogPostList and BlogPostDetail Templates πŸ’‘

Create the blog_post_list.html and blog_post_detail.html templates in the blog/templates/blog directory:

html
<!-- blog_post_list.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Blog Post List</title> </head> <body> {% for post in posts %} <h2>{{ post.title }}</h2> <p>{{ post.pub_date|date:"F d, Y" }}</p> <a href="{% url 'blog:blog_post_detail' post.id %}">Read More</a> {% endfor %} </body> </html>
html
<!-- blog_post_detail.html --> {% extends 'base.html' %} {% block content %} <h1>{{ post.title }}</h1> <p>{{ post.content }}</p> <p>Published on: {{ post.pub_date|date:"F d, Y" }}</p> {% endblock %}

Don't forget to create a base template (base.html) in the same directory:

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}{% endblock %}</title> </head> <body> {% block content %}{% endblock %} </body> </html>

Running the Server πŸ’‘

Start the Django development server:

bash
python manage.py runserver

Now, navigate to http://127.0.0.1:8000/ in your browser to see your blog in action!

Conclusion πŸ“

In this tutorial, we've explored the Django MVT architecture, creating a simple blog application along the way. You've learned about Models, Views, and Templates, and how they work together to create dynamic web content.

Now, it's your turn to create amazing web applications with Django! Happy coding! πŸŽ‰