Codementor Events

Let's talk Django!

Published May 24, 2020Last updated Aug 10, 2020
Let's talk Django!

Hi all !
Welcome to the blog post "Let's talk Django!"
If you are looking for a way to kickstart your journey with django then this blog post is for you.
In here and in upcoming blog posts I will be sharing with you all stuff Django.
I will follow a proper flow so that one can understand the basics of django, that is why I decided I should create a step by step series of blog posts for all.

Before we start
A bit about me !

I am a lead python developer at W3sols (A leading web and mobile app development company)

So let's begin by talking what is django ? 🧐

Well first things first it is pronounced as "jango" not "d jango" the "d" is silent.

  • Now, let's define it.
    Django is an open-source Python based web framework that follows an architectural approach called "Model-View-Controller" to help developers build web apps in structured way.
    Now, you may ask what I meant by structured ?
    For that we have to dig into what is "MVC or Model-View-Controller"

"MVC or Model-View-Controller" is an application design pattern that has three interconnected parts to it "Model", "View" and "Controller".

Screen Shot 2020-05-24 at 8.31.51 PM.png

In the image above I have tried my best to explain what "MVC" looks like.
Let's start undertanding it with what is a

  • MODEL
    A model is the data used by our code. Now, this data could be in the form of either any database, any document, image etc.

  • VIEW
    Whatever one sees on the web app's screens the objects, text, images etc. are rendered using a view. What is visible on the screen is displayed using a view.

  • CONTROLLER
    Name is self explanatory, it controls everything. It controls the the data flow from model to user. It is the place where whole logic exists. Let's take an example:

    • User fills a subscription pop up on the web app(view) with his email and presses Subscibe button.
    • The data is sent to code that resides in controller using post request. That means data is posted to our code.
    • Then the code in the controller takes over and process whatever it has recieved in that request. In our case it is user's email. It checks the email validity and stuff and if the email is invalid a response is sent back to view where the view displays a message "Invalid email". Ofcourse this can be done with frontend javascript validation as well but let's assume we are not using it. On the other hand if email is validated then we it moves forward to next steps that is saving that email to database.
    • The controller is connected to the model that will be a database that stores the data (email) being sent to it. If the code in controller succeeds with data saving process then a response with a message "Success" is sent to view and view displays "Thanks for subscribing".
    • Now, you may ask how does the view know what message it has recieved from controller? Well, that check can be coded in the view very easily. Will get to the coding part later in other posts for now let's clear our concepts.

I hope you got an idea now how MVC works. That is how django works as well in a structured way.

Let's answer another question now.

What part of Django is it's Model-View-Controller ?

Well, I will describe the whole django setup and it'sdirectory structure in my next blog. For now, let's have high level understanding of what are the parts in a django project that we label as it's model-view-controller.
Django has
- models.py -> MODEL
- template directory -> View
- views.py -> Controller

models.py

In the project structure models.py is where we define our database structure. We create models that act as tables for our database. So basically we programmatically create tables for our database in models.py which along with our database acts a MODEL in Model-View-Controller.
Wait database ????
Yes, database. The default database created with every django project automatically is a sqlite database. Now you create tables in models.py you have to store them in a database right! Don't worry we can configure a database of our choice.πŸ˜‰

template directory

In the project structure this directory is not present by default we have to create it ourselves. Inside the template directory is where all the code for your web page resides and what we use to create a web page?
"HTML".
All the .html docs reside in this directory. But syntax is not only the plain old html syntax. It has in addition to it "jinja2" templating system. It is a templatinng system used with python and in other python frameworks as well like Flask.
And Don't worry at all it is very easy.

views.py

Now, name can be misleading. views.py actually acts as a controller for our project. This is where all the logic for data processing, request, response exists.

So, that's all about conceptual overview of what django is.
A lot covered but trust me it is very easy. I hope I was able to explain well as I tried to break things as much as possible.

So, if you liked the way I explained stuff please like the blog and don't forget to comment below!

Discover and read more posts from Dharvi
get started
post commentsBe the first to share your opinion
Show more replies