Django way

Django way

Django Architecture

Django Architecture

Subscribe to my newsletter and never miss my upcoming articles

Ok you have decided to create your website using Django as the backend and so it will be a smart move to know how Django actually do the work underground when requests are being sent to it from the front end.

Django is said to be 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 is for Templates layer (HTML, CSS, JavaScript , static files etc).


When Django receive a request says, it 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 pick the view definitions attached to the url pattern which is airtime from above code. The airtime function is expected to be in a python view file in the same directory as the url file as defined on line two of the code above.

Now in the function called above, Django now made it's second move which is fetching or store data in/from the database if required by the view function 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
    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 move 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 raise an appropriate error option as far as DEBUG = True and if DEBUG is False it renders defined errors.


So the MVT structure is strictly adhere to by Django and it is known as Django way of performing task and thus as a Django developer the following are stuff you should be conversant with for proper operation of requests

  • URL Structure

Django various ways of writing URLs should be know 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 view or class based view and each of them has it's own strong and weak areas and as a beginner it will be best to be very much comfortable with function based view to give you better understanding of what is going on in the background of the class based view when you start to use them.

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 less 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 is expected to be know and majorly relating tables is a must also and how the values of the table are being stored and fetched should be learnt.

from django.db import models
class Airtime(models.Model):
    network = models.CharField(max_length=50)
    denomination = models.PositiveIntegerField()

    def __str__(self):
  • Templates Definitions

Presenting result to the website users is the end goal of the project and so how does view functions talk to templates should be known.

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.

Share this