Things to Know About Django Beforehand

Image by Kelly Sikkema on Unsplash

Choosing the appropriate technology stack is not easy, and it would be good to know if a certain framework suits your use case earlier, right? In this post, I will introduce you to Django framework, its qualities and I will explain what Django is suitable for.

What is Django

Django is a high-level web open-source framework based on Python. As stated on Django’s official website, the Django framework encourages rapid development with an emphasis on clean code. Django can be defined as “heavy-framework” because it adopts a batteries-included philosophy, meaning that all the common functionalities needed must come with the framework, instead of as additional libraries.

Why Django

The purpose behind the Django framework is to make the web development process easier and faster. The framework can be seen as a toolkit of components that can be used by developers when developing web applications. By using Django, developers can focus on the most unique features of their site, letting Django handle the rest (like authentication, security, caching, URL routing, etc.).  One of the most important points about Django is its flexibility. Django can be used to build almost any kind of web application, ranging from a cooking blog to a social networking site or an e-commerce platform.

MVT Design Pattern

MVT stands for Model View Template, and it is a software design pattern. For some aspects, MVT is similar to another pattern called MVC. In the Model View Template pattern though, the controller part is handled by the framework itself. By being a model-centered system, Django’s modules are highly modifiable and extensible as well. This design pattern potentially brings in other advantages, like concurrency support and decoupled architecture.

Django’s Qualities

When talking about technologies and web frameworks, features can tell if they are suitable for a project or not. Django in particular has a few relevant functional features that will be analysed in the following sections.

Security

Django uses the Object-Relational Mapper (ORM) for every database-related operation. In this way, the application is not dependent on which database is used and, at the same time, built-in protection against SQL injection attacks is provided. Django’s ORM prevents SQL injection thanks to strongly typed queries, as well as the special validators that can be additionally written. Cross-site scripting (XSS) attacks are partially prevented by Django’s template engine, because it handles the escaping of HTML characters, treating them as unsafe characters. XSS protection is only partial, meaning that other vectors can be used to exploit this technique (cookies for example). Other security features can be enabled using Django’s settings, like HTTP to HTTPS upgrade, HTTP Strict Transport Security and HTTPS-only cookies.

Scalability

Django adopts a shared-nothing architecture (SN). This kind of architecture is popular for web development, and it separates every component so that each one is independent of the others. The SN architecture makes Django able to easily scale based on operational needs, so it is possible to add caching servers, database servers and other components as the user-base grows.

Maintainability

Like many other programming languages, Python supports and encourages modularity. Being built-in Python, Django embraces this philosophy, and it encourages the development of maintainable and reusable code. Django adopts the Don’t Repeat Yourself (DRY) principle, which means that code won’t be duplicated but it’ll be reused instead. This concept allows the reduction of the codebase, and it also allows better collaboration between the developers.

Object-Relational Mapper

Object-Relational Mapping is a technique that provides a layer of abstraction that is used to interact with the application data from different relational databases like PostgreSQL, MySQL or SQLite. Thanks to this abstraction, developers don’t have to write SQL code. They can do CRUD operations using pythonic syntax. The main advantages of ORM are fast development and portability.

ORM Diagram ORM Diagram - Photo by Author

In Django, every database table is mapped to a Python object that inherits from models.Model Django class. The SQL to create the table is automatically generated by the ORM.

Suitability

To get a sense of what Django is capable of, it is useful to look at existing companies that use the framework in their products. As stated on Django’s official website, the Django framework is used by different big tech companies, like Instagram, Mozilla, Pinterest and others. Django is also used to build National Geographic and MacArthur Foundation’s websites. Social networks, web browsers, video entertainment and private foundations websites are just a few examples but Django can be used to implement almost any web application. There are certain cases though in which Django wouldn’t be a good choice, and in the following section we address these cases.

Limitations

Django may not be the right choice for small web applications. For example, if you have to build a website with a limited amount of features like a simple blog, Django is not the best choice. Due to the amount of source code involved, Django would introduce unnecessary overhead in terms of CPU usage and bandwidth as well. Django is typically used with medium or big projects, which involve multiple features as well as scalable solutions, so for a small project, you may want to look at other framework solutions, like Flask microframework. Another limitation of Django is that it is a monolithic framework. In software architecture, a monolith is a software application whose modules can’t be executed individually. A monolith can be seen as a single large app that must be deployed all-or-nothing. Due to this architectural choice, Django is affected by a few issues. One of the most relevant ones is that a single change in one of the modules forces the rebooting of the entire application.

Conclusion

Django is a well established Python-based web framework, rich in useful features and great learning resources. Although the framework is relatively old when compared to other modern commonly used web frameworks, its adoption still does have benefits but it depends on the specific use case. I hope that it is clear by know if it’s the right choice for you 😃.

Thank you for your time!

References

  • Jhonny Mertz and Ingrid Nunes. 2017. Understanding Application-level Caching in Web Applications: a Comprehensive Introduction and Survey of State-of-the-art Approaches. ACM Comput. Surv. 9, 4, Article 39 (March 2017), 32 pages. DOI: 0000001.0000001. Retrieved from: link
  • Nicola Dragoni, Saverio Giallorenzo, Alberto Lluch Lafuente, Manuel MazzaraFabrizio Montesi, Ruslan Mustafin, Larisa Safina. (2017). Microservices: yesterday, today, and tomorrow. Retrieved from: link