In this article and the upcoming ones, I will provide you with a basic introduction to what is the Djnago framework and how to create your first ever app in simple and straight forward steps, with the addition of explaining a bit more on each building block that the framework is made up of.
So, let's begin.
What is Django
Django is an MVT, high-level Python web framework that aims to develop dynamic web apps, sites. It was developed by smart-working developers and contributors for the main purpose of making web-development fun again. That means Django takes the bigger share of the hard-work when it comes to developing or writing your apps, leaving you with the easy and enjoyable task of watching your work come to life with no great hassle other than focusing on the big idea behind your app and making sure you're satisfied by the end of the long working hours.
Django is an open source and available for just about anyone on Github, with the ability to contribute to it. As it also comes with great documentation that makes understanding and practicing the framework easier.
Fair to mention, that it is considered to be one of the most popular web frameworks that are written in Python, and that is for many reasons I'll explain some of them below.
What makes Django special?
- Batteries are included
That means is that you as the user, instead of having to keep track of the countless libraries and functionalities needed to make your developed apps or sites top-tier and add them as you progress.
Django plans it all ahead for you, as it comes with built-in applications that make everything run smoothly, such as ORM (object-relational mapper or mapping).
- Easy Approach
Django makes it easier for developers to create websites that are quick, clean, and functional because of its easy approach to development, as it takes care of the hard part of it and does all the dirty work for you considering that there aren't any mandatory imports, third-party libraries and no XML configuration files.
- Adaptable and flexible
Because Django follows the design pattern of MVT (Model, View, Template) it does a good job at separating the different parts of your project. The front-end, back-end and database architectures are all independent of the others, making it easier for future improvement and replacement even.
- Actively and stably updated
As for some open-source software, the issue for their sustainability and consistency can be a big deal, but this is not the case when it comes to Django. Because it's actively being updated and tested by the huge community of developers and contributors who work to improve the code base with persistent work that make sure to create a reliable framework.
- Everything is within arm's reach
Due to the fact Django has a rich online community of developers and contributors, this makes it much easier for you as the user. Because, chances are whatever idea you might think of using this framework might have been deployed before or at least there's a project out there that will give you the lead on how to follow up on your own idea.
- Authentic Admin Interface
This is considered to be an advantage to Django, as it has a built-in admin interface that allows the trusted users to handle some tasks, such as:
- creating users and groups.
- managing the users and granting permissions.
Django's Popularity
As it has been established now, Django is one of the most well-known frameworks written in Python, and based on that knowledge and the discussed advantages above it landed itself a large set of users and clients as big companies have used it to create reliable and functional web applications, with a variation of their fields of expertise.
To mention a few of those companies:
- Disqus
Disqus is a platform that allows users to leave comments via a network. It is very popular among the blogging community due to its ease of use and impressive features. Disqus can quickly cater to the needs of its rising number of users thanks to Django's adaptability and a large choice of ready-to-implement solutions for over 50 million users.
I should also mention that Disqus is the largest project -so far- that was built on Django framework from scratch.
- NASA
The National Aeronautics and Space Administration’s official website is using Django as one of its framework alongside others to navigate users all over the world to browse Space related news. For more information about the frameworks and programming languages that are deployed by NASA, visit CODE.NASA.GOV
With the help of Django, connecting people and sharing life's precious moments is made a lot easier, as this very well-known social media platform that has millions of people using it every single day; to share photographs and videos, is relying on Django to handle all these interactions.
This very famous social media platform allows millions of users to browse through pictures and videos of their interests, mainly looking for inspiration or for the sole purpose of indulging in aesthetic visuals. Pinterest uses Django to handle all the various interactions done by the users.
- Youtube
This widely known video-sharing platform used to be built in PHP, but the YouTube team saw the need to improve its performance by adding new features and upgrading the already implemented ones in a short time, and that's why Django was considered to be the best framework for the job, due to the framework's optimal performance when it comes to handling the rapid audience growth of the platform and the seamless work it does in helping the developers create their projects.
Is Django an opinionated framework?
Web frameworks can either be opinionated or unopinionated:
Opinionated frameworks are the ones that have a certain idea about the correct approach to complete a task. And that's because for them, the appropriate or correct way to complete something is usually well-documented. However, they can also be less adaptable and flexible when handling challenges outside of their primary domain, with fewer options for components and methodologies.
Whereas, unopinionated frameworks are the ones that impose significantly fewer constraints on how to interconnect components to complete a task, or even what components should be utilized while doing so. They make it easier for developers to employ the best tools for a given work. However, there's a downside of having the user hunt down those components themselves which could be an added hassle.
Where does Django fall on the spectrum?
Django falls right in-between, as it's considered to be slightly opinionated. And by that, it means that it includes a set of features for handling web development tasks, as well as a few recommended methods for using them.
Django's way of interacting with the database
Defining ORM
Let me begin by defining a database, which is a systematically organized collection of data that allows for quick and secure data storage, access, and updates.
And as the name suggests; ORM (object relational-mapper) maps out the object attributes to table fields, hence the word relational which is connected to relational database: which is a collection of tables having a number of columns and rows.
The central objective of the ORM remains to be automatically transferring data between a relational database and an application model. The core advantage of ORMs is that they provide rapid and affective web development. ORMs make the projects more portable and handling the changes in the database much easier.
As can be seen in the above image, the ORM took care of the hassle of creating and storing the object's attributes in the table fields automatically without having to write SQL, considering that acquiring basic SQL knowledge is not enough while building your application, merely for the fact that SQL implementations differ from one another in different databases.
Why do we prefer ORM?
ORM has quite the advantages over the regular approach of creating a database, and those advantages are:
ORMs provide rapid and functional development, due to the fact that web developers don't have to acquire the regular skill and knowledge in SQL as it's crucial for the web development, by firstly creating classes and objects, then manually converting them into a database schema.
ORMs make projects more portable.
ORMs make it easier to change the database.
ORMs automatically create a database schema from defined models.
Django's ORM
Django's ORM can be considered to be a pythonic way to create SQL to query and manipulate your database and get results as you would regular SQL queries.
And this can be done by referring to models.py file in your Django project, and writing out your classes /models which will then be converted into a relational database with minimum hassle. An example of a model named Snack is provided below:
from django.db import models
from django.contrib.auth import get_user_model
class Snack(models.Model):
name = models.CharField(max_length=64)
purchaser = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
description = models.TextField()
calories = models.IntegerField()
sodium = models.IntegerField()
fat = models.IntegerField()
carbs = models.IntegerField()
protein = models.IntegerField()
def __str__(self):
return self.name
The Snack model is implemented as a subclasse of django.db.models.Model
, and can include fields, methods such as __str__()
.
Fields
As for the fields a model can have a number of them, where they would represent a column of data that we want to store in one of our database tables. And this database rows will consist of one of each field value. In the above code, those fields are:
- name
- purchaser
- discerption
- calories
- sodium
- fat
- carbs
- protein
Field Types
- CharField: is used to define short to mid sized fixed-length strings that are fixed if you provide an argument of
max_length
. - ForeignKey: is used to specify a one-to-many relationship to another database model.
- TextField: is used for large-length strings.
- IntegerField: is used for storing integer (whole number) values.
Methods
__str__()
method is a must have when writing any Python class. It is used to return a human-readable string for each object or model instance. It's preferred if it returns a title or name field from the model.
It's also important to realize the importance of Migrations when it comes for defining the database schema, as well as using them to maintain complex databases whenever changes are performed in them.
Conclusion
You might have come up with the idea that Django is a best friend for the smart-workers, who want to skip the additional steps of figuring out how to sort things completely on their own. Because this framework with all its built-in functionalities and advanced methodologies lets you get the job done without re-inventing the wheel altogether.