Django

Why Django?

Hey there, soon-to-be Web developers! Ever thought about how websites are developed? How people write thousands of lines of code in a few days? The process of developing a website is quite lengthy and mesmerizing as you see how writing a few lines of code can change how your site looks. The very first step towards developing a website is asking yourself:

“Which tool should I use to meet the needs of the website?”

How and When was Django created?

Django was created in the fall of 2003, when the web programmers at the Lawrence Journal-World newspaper, Adrian Holovaty and Simon Willison, began using Python to build applications. It was released publicly in July 2005. In June 2008, it was announced that a newly formed Django Software Foundation (DSF) would maintain Django in the future.

What is Django?

Django is a Python-based free and open-source web framework that follows the model-template-view (MTV or MVT) architectural pattern. Its main goals are simplicity, flexibility, reliability, and scalability. It is maintained by the Django Software Foundation (DSF), an American independent organization.

Did you know that the Django framework was named after guitarist Django Reinhardt?

Django Reinhardt

The architecture of Django:

Django follows the MVT architecture, which is a slightly different version of the MVC (Model-View-Controller) architecture that allows developers to change the visual part of an app and the business logic part separately, without their affecting one another. The three layers (Model, View, and Template) are responsible for different things and can be used independently.

Left: MVC Architecture | Right: MVT Architecture

Model

In Django, a model is a class that is used to contain required fields and methods. Each model class maps to a single table in the database. Django provides a database-abstraction API that allows us to create, retrieve, update, and delete a record from the mapped table.

View

A view is a place where we put our business logic of the application. The view is a python function used to perform some business logic and return a response to the user. This response can be the HTML contents of a Web page, or a redirect, or a 404 error.

Template

Django provides a convenient way to generate dynamic HTML pages by using its template system. In an HTML file, we can’t write python code because it is only interpreted by python interpreter, not the browser. While HTML is a static markup language, Python is a dynamic programming language. Django template engine is used to separate the design from the python code. It allows us to build dynamic web pages.

The main difference between MVT and MVC is that Django itself takes care of the Controller part, leaving us with the template. The figure below depicts a more accurate picture of the interactions involved among User and Django.

A more accurate picture of Django’s architecture
Then what should I do?

Trying to bend the definition of a Django view to fit a viewpoint inevitably leads to one of two things:

1. A confused programmer puts everything in the views module; or

2. The confused programmer says, “Django is too hard!”, and goes and watches TV instead

So, to get rid of all the M’s and T’s and V’s and C’s, the figure below presents a more holistic view of what Django’s architecture looks like.

A more holistic view of what Django’s architecture looks like

Note how a line is drawn between the client and the server-side. Like all client/server architectures, Django uses request and response objects to communicate between the client and the server. As Django is a web framework, we’re talking about the HTTP request and response objects.

It’s okay if your face looks like this after reading the above section. It’s a lot to digest but it gets simpler when you start using Django.

Now that we have got that out of the way, let’s get on and have a look at some of the advantages of using Django as the framework of your website.

Advantages of the Django framework

Ridiculously fast:

Django was designed to help developers take applications from concept to completion as quickly as possible. Django takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel.

Fully Loaded:

Django includes dozens of extras that you can use to handle common Web development tasks. Instead of having to write your own code, you just need to import the packages that you want to use. Django Modules/Packages span a wide range of topics that include:

  1. Authentication with auth package
  2. Admin interfacing with admin package
  3. Session management with Sessions package
  4. Managing temporary or session-based messages with Messages package, etc.

Python:

Since Django uses Python, it leverages some of Python’s fame and power to its own benefit. Python is arguably one of the easiest -if not the easiest- programming language to learn for beginners. The 2019 Stackoverflow Developers Survey revealed that Python is more common than PHP, and Python jobs pay better than C# and C++.

Left: Median of 55,669 responses | source: StackOverflow | Right: Percentage shown from 87,354 responses

Community:

Django’s community is one of the best things about it, they are very helpful and actively working on making the framework more beginner-friendly and stabilizing the framework while adding new features. Django’s documentation is quite thorough (and in my perspective one of the best documentations) and is useful as a standalone tutorial, it will help you wrap your head around various features so you can use it as a primary source of information.

Scalable:

Most developers, when thinking about picking up a framework, plan for the future of their website. That’s why picking a scalable framework is quite essential for many, and Django serves the purpose.

Built-in Admin:

The Django team was quite thoughtful when they created the framework, and they kept user and client satisfaction in mind. It’s quite unreasonable to create your own admin interface at the backend just to be able to manage your data with basic CRUD (Create-Retrieve-Update-Delete) operations. That’s why Django offers an administrative interface right out of the box that is both professional and versatile.

The landing page for the admin system in Django

Reassuringly secure:

Django takes security seriously and helps developers avoid many common security mistakes, such as SQL injection, cross-site scripting, cross-site request forgery, and clickjacking. Its user authentication system provides a secure way to manage user accounts and passwords.

ORM:

Django is valued for its object-relational mapper that helps developers interact with databases. An object-relational mapper (ORM) is a library that automatically transfers data stored in databases such as PostgreSQL and MySQL into objects. Django’s ORM ability to extract information speeds up web application development and helps developers build working prototypes in no time. Developers don’t necessarily have to know the language.

A typical representation of the MYSQL database and Python objects

Don’t repeat yourself (DRY):

Duplication (inadvertent or purposeful duplication) can lead to maintenance nightmares, poor factoring, and logical contradictions. Django framework follows “Don’t repeat yourself” principle as it concentrates on getting most out of each and every line of code by which we can spend less time on debugging or code re-orientation etc. In general, DRY code means every distinct concept and/or piece of data should live in one, and only one, place.

The code in the above diagram shows how Django uses the DRY principle by iterating over the dataset and printing info of individual elements instead of duplicating the code manually and changing the input for each element.

What is Django used for?

Django can be used for creating:

  1. Client relationship management (CRM) systems;
  2. Content management systems (CMS) for internal and commercial use;
  3. Communication platforms;
  4. Document administration platforms;

Among other things, Django is excellent for:

  1. Algorithm-based generators;
  2. Emailing solutions;
  3. Verification systems;
  4. Data analysis solutions and complicated calculations;
  5. Machine learning;

Who uses Django?

According to similartech.com there were 85,400+ websites built with Django as of May 2020. The sectors using Django the most are business and industry, arts and entertainment, internet and telecommunications, and shopping.

Distribution of websites developed under Django framework

Sites built with the Django Web Framework(Python-based development):

  1. Disqus
  2. Pinterest
  3. Instagram
  4. Dropbox
  5. Spotify
  6. Mozilla
  7. The Washington Post
  8. National Geographic
  9. The Onion
  10. NASA
  11. Reddit

Well, we have seen a lot about Django and its pros, but as every coin has two sides, Django too has some cons.

Disadvantages of Django:

Not suited for small-scale projects.

Django can sometimes be excessive. For example, if you need to design a simple chat, Django can be too big of a framework and you can instead go with Flask, a microservice framework.

Monolithic.

Some internal Django modules, such as ORM and forms, are hard to replace, requiring a lot of effort from your developers to change the internal structure.

Django’s behavior is sometimes hard to tune.

Some internal Django modules such as the admin panel, are hard to tune because of Django’s philosophy. For example, if you want to add a link, dynamic statistics, or something unique that isn’t included in the Django ecosystem, this can literally take hours.

However, Django’s advantages outweigh its disadvantages by a significant margin.

Wrap-up

Django is an excellent addition to projects that need to handle large volumes of content, user interactions, or heavy traffic, or deal with complex functions or technology (e.g., machine learning). Yet it is simple enough for you to develop small websites and then scale your project to a much higher level. That’s why Django is used by so many companies that vary in size and goals.

I would recommend you to go through the Django documentation and explore all the APIs and packages that Django provides and use them to build something practical.

An article by Mohit Jain. (Coding Club, IITG)

--

--

A series of short informative blogs where the best programmers have your back with all the new technologies you need help exploring. So dive in!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Coding Club, IIT Guwahati

A series of short informative blogs where the best programmers have your back with all the new technologies you need help exploring. So dive in!