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! π
In Django, the MVT architecture separates an application into three interconnected components:
A Model in Django is a Python class that represents a database table. It defines the fields, data types, and relationships between different tables.
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.
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.
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.
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.
<!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.
What are the three components of Django's MVT architecture?
Let's create a simple blog application with a list of blog posts and a detail view for each post.
First, create a new Django project:
django-admin startproject myblog
cd myblogThen, create a new app for our blog:
python manage.py startapp blogOpen the blog/models.py file and create the BlogPost model:
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.titleOpen the blog/views.py file and create the BlogPostList and BlogPostDetail views:
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})Create the blog_post_list.html and blog_post_detail.html templates in the blog/templates/blog directory:
<!-- 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><!-- 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:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>Start the Django development server:
python manage.py runserverNow, navigate to http://127.0.0.1:8000/ in your browser to see your blog in action!
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! π