The DJANGO Framework!
Having trouble starting with Django? Get familiar with it.
Haha..! Django seemed like this monster to me when I started working with it. Teasing me all the way with its code and heavy stuff!
So no worries now ;) if you feel the same.
We will be going to discuss the very basics so that we can have the real insights of the Django framework. We will not be doing the coding part though, we will cover all the things one should know before directly starting a project in Django!
So you will get an overview of this framework and an idea of how everything is working behind the scenes.
This will be our flow :
Let’s get started!
What is a Framework?
A framework is a platform for developing applications. It is built upon a programming language which provides functionalities and components to the user for fast and easy development so that he need not to write everything from scratch.
A bit technical ?
Let us understand in a simple way.
For example :
Do you know how bricks are manufactured? During the process of giving shape to the bricks the soil mixture is put into mould (frame) providing it with a proper skeleton, so that work is done within that frame only. Similarly, a web framework defines a basic structure for our project/application and the user can work(write codes) inside it.
This structure standardises our application to work within a frame/environment. It may include libraries, predefined modules helping the user to focus on app logic without worrying about managing the setups (this may include connection to the database, security issues, interaction with local system etc.).
Now the definition may seem simple to you. Give it a read again!
Django is a high-level python web framework used for rapid development of web applications and easy deployment as well. We will explore its features ahead.
DJANGO DOES A LOT FOR YOU !!
Okay, friends! Coming to an important part i.e. the M.V.T design of Django. M.V.T architecture is Model View Template-based architecture. It describes the cycle of requests and responses between the client and the server.
There are 3 components of M.V.T which are related as follows:
Models- Model helps to handle the database.
Views- Views present the business logic of the application i.e. what should be rendered to the user as a response for a particular request.
Templates- It contains the HTML files which are shown to the user as rendered by the view.
This is too straight forward!!
So, to understand how does MVT works we would have a quick view on the project structure of Django and some important files. Afterwards, we will move back to the working of MVT.
Starting a new project in Django(make sure you have installed virtual env and Django ). Write the given command on your terminal/cmd:
django-admin startproject myproject
cd myprojectThis is the project folder. The outer directory “MYPROJECT” acts as a container for the whole. It contains “manage.py” :
manage.py: It is Django’s command-line utility for administrative tasks.
We will be discussing some useful administrative commands with the flow of content.
e.g. the following command is used for starting the Django server :
py manage.py runserverThe inner directory “myproject” contains 4 files, our focus, for now, is on two files:
- settings.py:
This file sets up the environment and adds on needed for the development. It manages the interaction between the application and our local system and includes various installed apps/packages along with our Django app(other apps/package installed by the user has to be included here). It has got the settings for Database engine where we can connect our database, by default it is SQLite but Django also supports MySQL, PostgreSQL. The templates folder containing the HTML files is to be included in the TEMPLATES section.
- urls.py:
This file contains all of our routes/URLs which are matched with the user’s request and then mapped with the views.
Now we will start an app within our project myproject. Write this command on your cmd for the same.
py manage.py startapp myappThe myapp folder contains:
- views.py: In this file, we write views(functions or classes) to describe what has to be done for a request.
- models.py: We define the data models here i.e. model classes. Each class represents a table in the database.
NOTE: The following commands have to be run whenever we make a new data model or changes the existing ones.
Get SUSHMITA JOSHI’s stories in your inbox
Join Medium for free to get updates from this writer.
The command below generates SQL statements for creating tables in the database out the classes written in models.py
py manage.py makemigrationsAnd for the execution of the SQL statements, we run the following command.
py manage.py migrate- admin.py: Here we register the Models that we want to have on the admin-interface(discussed later) provided by Django.
How does it work?
Suppose you are the user, you open your Django project and run the Django server.
1. You hit a URL(request) on the browser.
2. This URL is matched with all the URLs you have defined in your urls.py file in the project when a correct match is found it is mapped with a view in the views.py.
3. The logic/code written in that view is executed(may involve queries for data access from the database through models) then the response in the form of an HTML page.
This is how a response is sent to the user.
MVT software design used in Django is slightly different from the MVC (MODEL VIEW CONTROLLER)design pattern mostly used in Spring framework of Java.Read more about MVC here.
Okay, friends! Till here we have learned how everything happens in Django.
Now, we will be discussing in brief about Django ORM.
Generally, to access the database from an application we have to write many complex SQL queries for different operations. This becomes hectic and consumes a lot of time. Django makes it very easy for us!
Django’s ORM, the Object Relational Mapping provides us with a way to retrieve, update, create and delete data objects using database-abstraction API. With the help of ORM, we can use functions and Querysets with model objects for making different queries.
Next part is about a powerful and very useful feature of Django i.e. the Django admin interface.
- The Django admin site has the Models which we register in the admin.py file.
- After creating a superuser we can log in to the admin site. You can go to the admin site by making a request with this URL endpoint.
path('admin/', admin.site.urls) - The superuser can manage the data of the registered Models and can also give permissions to other authenticated users to have access to the interface.
Coming to the last and an interesting part. Although it is not as much of concern if you are an absolute beginner, so, you can skip this if you want.
Recall…there is a file wsgi.py in our project folder. Let us check it out.
WSGI stands for Web Server Gateway Interface. It is implemented in python to provide an interface between web server and application server or the web application framework.
Hey! hey! hold on... Web servers? Application servers?🤔
But we know only about the Django server.
Django’s built-in server is a lightweight server that is able to work in testing and development only. It cannot handle a lot of requests, may not provide good security, or become slow in the production environment.
And what if we still use it…? Then…we may end up like this :)
So, what’s the solution?
Here comes the role of a Web server and Application server.
A web server is a server which works on HTTP (HyperText Transfer Protocol) and used to take requests, render responses(only the static content such as HTML, CSS, Images etc) to the client. However, application servers deal with the business logic of our application and how to run dynamic content on the webpage. It can use a number of protocols including HTTP.
Some Application servers may consist of a web server. In such cases, we need not use web servers explicitly.
For using Django in production we use mostly Nginx web server and Gunicorn as an application server.
You can read here more about how Nginx and gunicorn work together.
Okay, friends, that’s it! But Django isn’t this much, there is a lot..lot..more to explore in Django.
As you are now familiar with Django, you can start coding your first project.
Hope you enjoyed it!
Thank You.
Happy Coding :)






