Python is one of the most famous programming languages in the world and it was the second most used language in GitHub projects in 2017. So if you’re interested in Python and in web development, you’ve most probably heard of Django. The rising fame of web frameworks nowadays makes it quite confusing to learn a new framework and choose which one you want to work with. Today, we take an extensive look at Django to be able to wrap our heads around this amazing technology.
What is Django?
Django is an MVT web framework used to build web applications. It defines itself as a “batteries included” web framework, with robustness and simplicity to help web developers write clean, efficient and powerful code. It is among the most famous web frameworks out there in the world and it’s one of the most used frameworks as well. It’s used by Instagram, Youtube, Google and even NASA for their website. So let’s break it down even further to learn more about it.
Django follows an MVT architecture which stands for Model-View-Template. MVT is a Django variation of the famous MVC structure, that’s why you’ll feel it’s quite analogous to how other frameworks work. When the Django server receives a request, the URL router maps the request to the appropriate view. The view then fetches the data through the models, fill the template and send it back to the user.
What is Django Used for?
Django’s model makes use of a powerful ORM layer which simplifies dealing with the database and the data and accelerates the development process.
Without Object-Relational-Mapping, developers would have to create the tables themselves and define the queries or procedures which sometimes translates to the hefty amount of SQL that is prone to be complex and hard to track.
The ORM layer lets you write all the table definitions in simple python code, and it takes care of translating that to the appropriate query language chosen, and it also facilitates the CRUD operations.
In fact, the developer doesn’t necessarily need to know the complex SQL altogether or what it translates to, though, it worth noting that understanding SQL would allow you to write better and faster queries and also make your website more secure.
Unlike other frameworks, the models are all placed in one file, conventionally, models.py, which might make it feel crowded for bigger projects.
Django supports many database systems. SQLite is really good for testing and development because it could be used right out of the box without having to install further software. For production, you can go for MYSQL or PostgreSQL, and if you’re looking for a NoSQL database, you can use MongoDB with Django, and here’s a further read on the topic.
The template layer is used to separate the data from the way it’s actually presented and viewed by the user. The template layer is similar to the MVC’s View layer. If you’re familiar with templating in other languages, it’s kind of the same in Django; you use an HTML like syntax that is later compiled to HTML with all the respective data injected. Of course, there are formats for templates other than HTML, if you want to generate XML documents or JSON files, etc...
DRY is one of Django’s core template design principles and it’s a design pattern that stands for Don’t-Repeat-Yourself. It’s exactly what it sounds like, it means that you shouldn’t, at least in most cases, by copying and pasting the code. Instead, your template, for example, should be divided into reusable components such as the side navigation bar, the main navigation bar, the header of the page, the footer of the page and so on. This minimizes repetition and makes for writing efficient and cleaner code.
One of the things that Django makes distinct of itself is how seriously it considers security. This indeed affects the writing of the template.
In Rails, for example, you can write ruby code inside your views -not saying it’s something you should do-, and you can also assign values to variables.
In Django, this is not the case at all, you cannot run python code in the template and you can’t assign a value to a variable. Simply put, Django forbids code-execution in the template layer and only gives access to display logic, which is a simple yet extremely effective solution to many web vulnerabilities.
The View in Django is the business logic layer. It’s responsible for processing the user’s request and sending back a valid response. It fetches the data from the model, gives each template access to specific data to display, or it could perform some processing over the data beforehand. Nowadays, Django’s views can be functions processing the request and returning a response, or they can be classes capable of much more in a similar manner to Laravel and Rails controllers.
The URL router
The URL router in Django is more complicated than in other frameworks, say Rails or Laravel. The problem with it is that it uses regular expressions which are not easy to use for beginners. However, the structure of the URL router itself is not complicated at all, it’s just the syntax that you might not be comfortable with at first.
Advantages of Django
1. Batteries included
Django prides itself as a batteries-included framework. What that means is that it comes with a lot of stuff out of the box, that you may or may not use depending on your application. Instead of having to write your own code (the power), you just need to import the packages that you want to use.
It’s a part of the convention over configuration paradigm that Django is part of, and it allows you to make use of the solutions implemented by world-class professionals. Django batteries span a wide range of topics that include:
- Authentication with auth package
- Admin interfacing with admin package
- Session management with Sessions package
- Managing temporary or session-based messages with Messages package
- Generating Google sitemap XML with Sitemaps package
- Postgres special features with Postgres Package
- Hooking into “types” of content with content types framework
Since Django uses Python, it leverages some of the fame and power of python to its own benefit. Python is arguably one of the easiest -if not the easiest- programming language to learn for beginners, and it’s also quite popular in introductory computer science courses around the world. The 2017 Stackoverflow Developers Survey revealed that Python is now more common than PHP and Python jobs pay better than C# and C++.
Django’s community is one of the best things about it, they are 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 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.
Most developers, when thinking about picking up a framework plan for the future in their choice. That’s why picking a scalable framework is quite essential for many, and Django is just that. It allows you to take a lot of different actions regarding scalability, such as running separate servers for the database, the media, and the application itself or even use clustering or load-balancing to distribute the application across multiple servers.
5. 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 operations. That’s why Django offers an administrative interface right out of the box that is both professional and versatile, according to the documents the developer can now develop with the presentation in mind.
Disadvantages of Django
While Django is an amazing framework, there are a few cons that may or may not be a problem for you. First, the URL specifying with regular expressions is not an easy task to accomplish, at least for beginners. It also feels a little bloated for small projects, and some people find it quite populated with big projects since models, for example, are all included in a single file. Template errors fail silently by default, so if you don’t know that, you might waste a lot of time trying to figure out what’s wrong with the application, or even worse, you might not even know that your application has a problem. It’s also a strongly opinionated framework, which gives it a monolithic feeling. There’s one popular and advised way of doing things and you’re supposed to follow it.
How to learn Django
First, you should start with Python and understand the natural flow of web applications. You can find the programming community’s best recommended Django tutorials on Hackr.io.
You can pick the most upvoted tutorial or select tutorial as per your learning style: Video-based tutorial or a book; for beginners or for advanced learners. You can begin with CS50’s lectures on web development and you’ll also get briefed on Python and how to use it.
To master Python, Bill Weinman's course on Lynda is a masterclass and you can also check Udemy for courses on both Python and Django. The official documents of Django are also a good place to start with and it provides thorough tutorials as we said before, not just API usage.
People are also reading: