Table of contents
No headings in the article.
Welcome developer, in the last post I introduced Django to you with its pros and cons but before you start using Django it's very important to understand how Django processes any HTTP request to give the necessary HTTP response. At the end of this post, you should have an understanding of Django MVT architecture for better coding and website performance.
Django is made of three layers which in short is MVT structure i.e. M for the database layer of the framework, V for the controller layer( routing and view functions) of the framework, and T for the Templates layer (HTML, CSS, JavaScript, and Images).
So when Django receives an HTTP request that says myvtu.ng/airtime, its first move is to take the subdomain which is airtime in the given address, and then it goes to the controller section which has both the URL (uniform resource locator) and the view definitions and check the defined URL patterns against the subdomain given
from django.urls import path
from . import views
urlpatterns = [
path('airtime/', views.airtime, name = 'airtime')#matched pattern
path('airtime/details/', views.airtime_details, name = 'details_airtime')
path('details/airtime/', views.airtime_values, name = 'airtime_details')
]
So with a matched pattern, Django now happily picks the view definitions attached to the URL pattern which is airtime from the above code. The airtime function is expected to be in a Python file named views in the same directory as the URL file as defined on line two of the code above and such function should take the request parameter as its first argument.
Now in the function called above, Django makes its second move to perform what the request asks for which can be fetching or storing data in/from the database as shown below
from django.shortcuts import render
from .models import Airtime
def airtime(request):
# database query (fetch)
queryset = Airtime.objects.get(network = 'MTN', denomination = '100')
# database save data i.e stores data in the database
data = Airtime.objects.create(network = 'Glo', denomination = '200')
return render(request, 'airtime.html', {'mtn':queryset})
When this is done, it is now the turn of the third movement which is presenting the data fetched or stored to the client using the Django templating system with the airtime.html file specified on the last line of the code above.
In case of any error at any point of the steps, Django raises an appropriate error option as far as DEBUG = True and if DEBUG is False it renders defined errors.
So the MVT structure is strictly adhered to by Django and it is known as Django's way of performing tasks thus as a Django developer the following are stuff you should be conversant with for proper operation of requests
- URL Structure
Django's various ways of writing URLs should be known from non-parameter URLs to parameter URLs to extra argument URLs.
urlpatterns = [
# non-parameter url
path('home/', views.index, name = 'index'),
# parameter url
path('home/<int:message_id>/', views.message.as_view(), name = 'message'),
# extra argument url
path('home/', views.QuizeView.as_view(), name = 'quizes', {'foo':'bar'})
]
- Views Definition
majorly in Django, we have function-based views or class-based views and each of them has its own strong and weak areas, as a beginner, it will be best to be very much comfortable with function-based views to give you a better understanding of what is going on in the background of the class-based view when you start to use them because class-based views made work easy but also have abstractions which seriously require your understanding in case of debugging.
from django.views.generic import ListView
from django.shortcuts import render
# Function Based View
def airtime(request):
# database query /search
queryset = Airtime.objects.get(network = 'MTN', denomination = '100')
return render(request, 'airtime.html', {'mtn':queryset})
# Class Based View
class BookListView(ListView):
model = Book
- Model Definition
With Django, you worry very little about writing pure database code as the model structure will be converted to pure database code being used as specified in the settings file.
Various fields in Django are expected to be known and majorly relating tables is a must also and how the values of the table are being stored and fetched should be learned.
from django.db import models
class Airtime(models.Model):
network = models.CharField(max_length=50)
denomination = models.PositiveIntegerField()
def __str__(self):
return self.network
- Templates Definitions
Presenting results (the response of the HTTP request sent) to the website users is the end goal of the project and so how does view functions talk to templates should be known. Here you will work with the Django template engine, serve static files (images, CSS, Javascript, etc)
Wow, you made it to the end ๐. In the coming articles, we are going to look into these three layers fully one after the other, and get the full gist.
Thank you for reading.