Django

What is Django Used for ?

What is Django_.png

In this blog, I'll be discussing what is Django, how does Django work, and what is Django used for. I will also be going over some of the key points on why it is so famous also mentioning some of the major companies and platforms that are running on Django.

What is Django?

Django is a high-level, open-source web framework based on Python language. The first release for Django came on 21 July 2005 and has evolved ever since into a huge ecosystem. Mainly Django is used for rapid development, maintainable, clean design, and secures websites.

Currently, Django has 3 major released versions

  1. Django 1.x
  2. Django 2.x
  3. Django 3.x (Latest)

Django 2.0 beta was released in April 2019, which was the first time Django broke the 1.x series and gave a higher performance beat release.

Django follows the model-view-template architectural pattern. Django was developed by an online news operation team with an aim to create web applications using the Python programming language. It has templates, libraries, and APIS which work together. You can install and use Django once you have installed Python.

What is a Web Framework?

A web framework or web application framework is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Some other common web frameworks include: "Ruby on Rails", "Angular", "Laravel", "Flask".

What is Django Used for?

The most common usage of Django is

  1. As a Web Framework (For Websites)
  2. As a REST backend (For Rest APIs)

Mostly Django is considered as a backend due to the language Python. as it is very widely used and has a huge amount of well-documented packages to use out of the box. Moreover, if the project grows you can leverage the machine learning and data mining techniques of python packages keeping your stack the same as the backend.

Coming towards the usability part of it, Django makes it easier to build better Web apps more quickly and with less code. With Django, you can take Web applications from concept to launch in a matter of hours. 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. Django is designed to get the job done as quickly as possible.

It lessens the workload and takes care of user authentication, content administration, site maps, RSS feeds, and many more tasks — right out of the box. Django can be used to create REST APIs and work ridiculously fast. Django is very versatile in nature.

Some of the most famous apps built with Django includes:

How Django Works?

django application flow.png

MVT (Model-View-Template)

Django uses MVT (Model-View-Template) rather than the old methodology of MVC (Model-View-Controller). As the name suggests, MVT has the following 3 parts :

Model:

Model is a built-in feature of Django that helps you create tables and fields of the database. Usually, we use SQL for creating, deleting, and editing any table or field in the database but it involves rather complex queries. Django has it very easy through its models. It looks something like this :

from django.db import models

class Article(models.Model):
pub_date = models.DateField()
headline = models.CharField(max_length=200)
content = models.TextField()
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

def __str__(self):
return self.headline

Each "Class" represents a table in the database and each field represents a column in that particular table. Django automatically adds a "primary key" field to the model. In short, a model works pretty much the same as SQL but in a much simpler way.

View:

Views are the real back-end of the project. Mainly, it is a Python function that takes Web requests and returns a HttpResponse. The view function takes the template, renders it, and returns it as a HttpResponse. It can also raise exceptions such as Http404. A view looks like this :

from django.shortcuts import render

from .models import Article

def year_archive(request, year):
a_list = Article.objects.filter(pub_date__year=year)
context = {'year': year, 'article_list': a_list}
return render(request, 'news/year_archive.html', context)

This example is the extension of the previously used example in the model section. Let me break this one down for you.

It imports the Class or Table "Article" from models.py. The year_archive function takes two parameters :

  1. request: It is used in every function in which a request is made.
  2. year: It is a positional argument passed in the URL

year_archive function filters the articles on the basis of the published year defined in "pub_date" field of Class "Article".

Context:

Here, Context is a dictionary with variable names as the key and their values as the value in this format: {'key':value}. When it is written in template file as {{ key }}, it will be replaced with "value". In this particular case {{article_list}} will be replaced with a_list which contains a list of filtered articles. This dictionary is passed to the template.

Template:

A template is a text document that is rendered by views with context. It is written in HTML, CSS, JS for static parts, and for the dynamic parts of the page, it uses Django template language (DTL) and Jinja. Templates use the DRY ( Don't Repeat Yourself ) method so you don't have to type the basic header, footer stuff over and over on every single page. Moreover, you can put some code in a file which you will be using oftentimes and use it whenever you need it by just typing

{% include "fileName.html" %}

Getting back to our example, have a look at this template file "year_archive.html" previously used in views.

{% extends "base.html" %}

{% block title %}Articles for {{ year }}{% endblock %}

{% block content %}
<h1>Articles for {{ year }}</h1>

{% for article in article_list %}
<p>{{ article.headline }}</p>
<p>Published {{ article.pub_date }}</p>
{% endfor %}
{% endblock %}

It extends on the base.html file which means that it will include everything that is present in base.html file. It will show the list of articles filtered in views through variable article_list. A for loop is used in Jinja which prints the "headline" and "pub_date " of each article.

NOTE: headline and pub_date were declared in models.py.

Fast and Easy

Django focuses on DRY(Don’t Repeat Yourself ) philosophy which means developers can reuse existing code and focus on the unique one. In general, DRY code means all uses of data change simultaneously rather than a need to be replicated and its fundamental reason to use of variables and functions in all programming.

Reliable

Security is also a high priority for Django. It has one of the best out-of-the-box security systems out there, and it helps developers avoid common security issues, including

  • clickjacking
  • cross-site scripting
  • SQL injection.

Django often releases new security patches. It’s usually the first one to respond to vulnerabilities and alerts. Django is time- and crowd-tested. It has a big, supportive community accessed through numerous forums, channels, and dedicated websites.

Operating System

Django framework runs on any platform like PC, Windows, Mac, Linux, etc. It provides a layer between the developer and database called ORM (object-relational mapper) which makes it possible to move or migrate our applications to other major databases with few lines of code change.



About author

shahraiz ali.jpeg

Shahraiz Ali

I'm a passionate software developer and researcher from Pakistan. I like to write about Python, Django and Web Development in general.



Scroll to Top