Pros and Cons of Django as web framework for Python developers
Django describes itself as "the web framework for perfectionists with deadlines". It was designed to help Python developers take applications from concept to completion as quickly as possible.
It allows quick development if you want to create a CRUD application with batteries included. With Django, you won't have to reinvent the wheel. It just works and lets you focus on your business logic and creating something users can use.
Benefits of Django
“Batteries included” philosophy
The principle behind batteries-included means common functionality for building web applications comes with the framework, not as separate libraries.
Django includes dozens of functionality you can use to handle common web development tasks. Here are some high-level functionalities that Django provides you, which otherwise you have to stick together if you were to use a micro-framework instead:
- Database migrations
- User authentication
- Admin panel
Django as a framework suggests the correct structure of a project. That structure helps developers in figuring out how and where to implement any new feature.
With a widely accepted project structure that is similar to many projects, it is much easier to find online compatible solutions or ask the community for help. There are many passionate Python developers who will help you solve any issue you may encounter.
Django applications (or apps for short) allow developers to divide a project into multiple applications. An app is anything that is installed by placing in settings.INSTALLED_APPS. This makes it easier for developers to add functionality to the web application by integrating external Django applications into the project.
There are hundreds of reusable modules and apps to speed up your development. Check Django Packages website.
Secure by default
Django provides good security protection out of the box and includes prevention mechanisms for common attacks like SQL Injection (XSS) and Cross-site Request Forgery (CSRF). You can find more details in the official security overview guide.
REST framework for building APIs
Django REST Framework, typically abbreviated "DRF", is a Python library for building APIs. It has a modular and customizable architecture that works well for both simple and sophisticated web APIs.
DRF provides a set of authentication and permission policies out of the box. It is a flexible, full-featured library with modular and customizable architecture. It comes with generic classes for CRUD operations and a built-in API browser for testing API endpoints.
GraphQL framework for building APIs
Large REST APIs often require a lot of requests to different endpoints to retrieve all required data. GraphQL it's a query language that allows us to share related data in a much easier fashion. For an introduction to GraphQL and an overview of its concepts, please refer to the official GraphQL documentation.
Graphene-Django provides abstractions that make it easy to add GraphQL functionality to your Django project. Regular Django models, forms, authentication, permission policies and other functionalities can be reused to build GraphQL schema. It also provides a built-in API browser for testing API endpoints.
Disadvantages of Django
Django ORM, created before SQLAlchemy existed, is now much inferior to SQLAlchemy. It is based on the Active Record pattern which is worse than the Unit of Work pattern adopted by SQLAlchemy. This means, in Django, models can “save” themselves and transactions are off by default, they are an afterthought. Read more in Why I sort of dislike Django.
Django evolves slowly
Django is large and is considered monolithic piece of software. This allows the community to develop hundreds of reusable modules and apps but has also limited the speed of development of the Django. On top of that Django needs to maintain backward compatibility, so it evolves slowly.
Summary - Should I use Django as a Python developer?
While Django ORM is not as flexible as SQLAlchemy and the large ecosystem of reusable modules and apps slows down framework evolution - clearly Django should be the first choice web framework for Python developers.
Alternative, light frameworks, like Flask, while offering a retreat from Django large ecosystem and configurations, in the long term can require much more extra libraries and functionality, eventually making many experienced Python developers finding themselves wishing they'd started with Django instead.
Django project's stability and community have grown tremendously over the past decade since the framework's creation. Official documentation and tutorials are some of the best anywhere in software development. With each release, Django continues to add significant new functionality.
If you liked this article, you should check out 7 Common Mistakes That Django Developers Make.