Other recent blogs
This blog was initially published on www.tivix.com(a subsidiary of Kellton). As a result of a merger of Kellton and Tivix all the content hosted at tivix.com has been merged with www.kellton.com. Happy Reading!
In this post we’ll look at web frameworks that are powered by one of the world’s most popular programming languages: Python. In particular we’ll focus on two of Python’s most popular web frameworks: Django vs Flask.
In web development there is never going to be a ‘one size fits all’ solution for every project. Both of these frameworks offer a different set of advantages and use-cases, which we’ll explore in this post. Read on to discover which framework is the better choice for your project, and why.
Django vs Flask: Why use a web application framework?
Using a web application framework can speed your development time, letting you focus on the custom parts of your application instead of redeveloping the common scaffolding that all web applications need.
In the Python development world, two of the major model-view-controller (MVC) frameworks to choose between are Django and Flask. While both support web application development, they have different philosophies that make a big difference in how they help your team complete its project.
So, Django vs Flask - which of these python web frameworks is a better fit for building complex web applications?
Django is a mature python web framework, with its first release back in 2005. It's designed to be ridiculously fast, reassuringly secure, and exceedingly scalable.
There's so much included in Django that it's often described as "batteries included"— it ships with all the features you need to implement a modern web application. Some of Django's most important features include support for:
- Authentication. Homegrown login controls are notoriously insecure. Use the built-in authentication support in Django to properly implement login, logout, and account management functionality.
- Security. Django's support for security doesn't stop with authentication. Applications developed with Django are protected against common attack vectors such as cross-site scripting and SQL injection.
- Object-relational mapping. With most data still stored in relational databases, built-in functionality for retrieving and working with data without writing SQL can be a major time-saver.
- Templates. Easily generate dynamic HTML with a template language like Jinja2.
- Forms. Interactive websites depend on forms to collect data from users. Django's forms functionality generates HTML forms, validates user data, and transforms it into Python types. Django can do all of this generation, validation, and transformation with built-in components that simplify many of the common data input and cleaning tasks.
Additional features in Django support the automatic creation of admin interfaces and advanced internationalization and localization features that allow your web application to serve different languages, time zones and date/number formats.
So, in the Django vs Flask debate, all of this functionality makes Django very powerful. It's opinionated and has strong guidelines for how to structure a web application. Its time-saving features can present a learning curve for developers just picking up the framework. All of its features and functionality are there whether you need them or not.
If you don't want to do things the Django way, you can disable those features, but you need to understand how they interact with the rest of Django and be careful not to break something else. If there's functionality you need that isn't built in, you can add it with third-party extensions like the Django REST Framework or customize yourself. There's also an active community you can draw on to get support.
Flask is a few years younger than Django and has an active, but smaller, community. Its website describes it as fun and easy to set up.
The difference in the way Flask describes itself and Django describes itself gives a flavor of the difference between the frameworks themselves. Unlike Django, which is all-inclusive and knows it, Flask is designed to be a minimal microframework, offering an extensible, do-it-yourself solution, without a lot of opinions about how you implement your system. Its built-in functionality includes:
- an included development server and debugger
- support for unit testing
- RESTful request handling
- secure cookie handling
The micro-nature of Flask means there's a smaller learning curve than with Django, and it's easier to get started building a small application. It can be extended with third party packages, meaning you can choose the solution that best matches your needs; there's a large set of available extensions to handle functionality like database access and authentication.
Having to choose the right extension adds complexity to developing applications. You have to pick the right components and you have to configure them and make sure they all work together properly.
You have power and you have control, but you can end up adding so many third party products for core functionality that you end up effectively recreating Django.
If you’re looking to build a project using Django or Flask, it makes sense to consider the talent pool for each technology, and whether the framework will be able to be easily supported by new hires, freelancers or web development agencies.
Python is one of the most popular programming languages, so you won’t have any difficulties finding a Python developer, but whether they have expertise in Django or Flask is a different situation.
This is one big win for Django, it has a large and very active community. With tens of thousands of developers across the world. Flask has a dedicated, but far smaller community. Meaning it might be trickier to find developers with Flask expertise to staff your project. Whereas if you’re building a product with Django, it should be relatively easy to find developers or Django development companies that can support your product as it scales.
Django vs Flask Performance
When considering Django vs Flask, each presents a rapid development experience. But aside from how quickly you can build an application, you might want to consider how fast the application you’ve built is going to be.
Python web framework benchmarks indicate that Flask is slightly faster at loading information from a database. This is to be expected, as Flask is a microframework, meaning there is less code needed to execute it’s commands.
But aside from a raw speed perspective, forward-thinking developers need to think about how applications will perform at scale. Django has a clear advantage here, it’s feature-set make it an ideal choice for building large, enterprise-level apps.
While Flask is faster, Django is by no means slow, as is proven in production by huge services like Instagram and Youtube which use the framework.
Advantages of Django
- Build apps fast: Django calls itself “The web framework for perfectionists with deadlines”, which certainly rings true if you’ve ever worked with it. Django has been designed from the ground up to give devs all that they need to rapidly build performant web applications. It’s an ideal option that can save your team time and money.
- Security as standard: Django comes with a great set of security tools straight out of the box. These tools offer protection from many common attacks such as SQL injection, Clickjacking and Cross site scripting.
- Plug and play: Django comes with many different applications, such as support for authorization or sending emails that can be easily plugged into projects to accelerate development or enhance existing features.
Rich ecosystem of supporting libraries, tutorials and documentation
Django was released in 2005, 5 years prior to Flask. It has since grown to become an incredibly popular choice for building web applications. It is used by many popular services like Pinterest, Instagram and Bitbucket. Along with this popularity at larger companies, the homegrown Django community has been busy building and releasing libraries and packages to save you time and effort in your project.
Kellton has built several open source Django projects. Including:
- Django-rest-auth provides a set of REST API endpoints for Authentication and Registration and
- Django cron, an app to automate and schedule cron tasks.
Django REST Framework
The Django REST Framework one of the best tools we know of for building web APIs. It includes authentication packages for OAuth1a and OAuth2. It is extensively customizable, and comes with comprehensive documentation to aid development. Django REST Framework is used in production by many large organizations such as Eventbrite and Mozilla.
Updates move slowly
Since Django aims to maintain backwards compatibility, updates are slow. However, this backward compatibility can be a big advantage as your legacy code is less likely to break due to future updates of Django.
Stable and proven in large-scale production environments
There’s a good reason that massive services trust Django, it’s incredibly stable and reliable. Making it an ideal choice if you envision your app growing to a significant size.
Cons of Django
- Django can be overkill for smaller projects: Django is feature-rich, but if you’re just looking to build a simple, small-scale web application, you might be better served by using a more lightweight framework like Flask or Pyramid. For many application-types, you simply won’t need all of the components that Django offers.
- It’s quite monolithic: Django’s nature encourages large applications, in which both backend and frontend functionality depends on the Django ORM. All components get deployed together. If you’re determined to build your product using a Microservices architecture that separates monolithic apps into smaller self-containing apps, Django might not be the best choice.
However, despite its monolithic nature, it is certainly possible to build Django applications that embrace Microservices.
Advantages of Flask
- It’s Simple: If you have a good grasp of Python, you should be able to easily build a basic application with Flask. It isn’t as opinionated as Django, so there are less coding standards to adhere to. But, as we explore later, this can be a double edged sword. Flask can make it easier for inexperienced devs to write inferior code.
- It’s very lightweight: If you’re building a smaller web application, Flask is ideal. It’s super lightweight, meaning it doesn’t come bundled with a bunch of extra features that go unused. It ships with the minimum that you need to build an efficient web application, and nothing more.
- Flexibility: Adding to the above point, Flask’s lightweight nature means it is pretty easy to customize large parts of it, without accidentally breaking things. On the flip side, if you find yourself needing to build out a bunch of custom features, it might make sense to start with a framework (like Django!) that includes them as standard.
- Performance: Since there are lower levels of abstraction with Flask, it tends to perform a little better than Django.
Cons of Flask
- Not easy to scale: Flask is a good fit for smaller projects, but when it comes to scaling them up, you might run into difficulties.
- Easier to write bad code: Since Flask follows few conventions, it’s easy for inexperienced developers to write code that can make your codebase confusing and potentially harder to maintain. In comparison, Django is very standards-based so specific coding conventions must be followed in order to make things work.
- Few bundled tools: Flask doesn’t come bundled with a huge amount of tools, this means you might have to build certain features for yourself, or seek out 3rd party libraries, which can vary significantly in their reliability.
The power of Django admin
Django comes bundled with Django admin, which allows devs to quickly spin up new site areas. Projects can be created quickly, and even split into new applications within one project. This is ideal if business requirements change and you need to change the structure of your web application.
Django admin is very powerful, allowing you to seamlessly create a backend system for essential functions like user authentication, handling forms and validating data input. Django even includes a simple interface to manage this data.
Flask comes with Flask Admin which helps you solve a lot of the same problems, albeit in a less opinionated way. Flask’s admin tool is more flexible, but does not come with the same comprehensive feature-set as Django’s admin tool.
Django admin isn’t very flexible out of the box, but it does its job well.
Django vs Flask: Other Python Web Frameworks to consider
Pyramid’s aim is to keep things simple. It works well for both small and larger applications. Positioned as “by artisans for artisans”, it’s not as popular as Django or Flask, but can be a useful tool for certain situations.
Unlike Flask, which is a ‘microframework’, and Django which is a ‘megaframework’, Pyramid aims to land somewhere in between. It allows you to start small, but can smoothly scale as your application grows.
Key features include:
- Built-in solutions for templating, databases, and security
- High-quality documentation
- Convenient scaffolding to generate sample projects
- Several form systems
- Function decorators (if you’re a python dev you’ll understand)
Bottle is an interesting microframework. It’s unique in that it implements all of it’s functionality from a single source file, meaning it has no dependencies. It’s small and ideal for rapidly prototyping your web application.
Key features include:
- Fast built-in template engine
- Convenient utilities such as file uploads, headers, cookies and more
- Built-in HTTP development server
Django vs Flask: The Final Decision
There are plenty of major websites built with both of these Python frameworks. Many developers recommend using Flask for early prototyping, because it's quick to get started with, and then building the production application in Django, because it has built-in all the features you need to make the application production-ready. Of course, that means you end up building the application twice.
And it's just as easy to argue that you should build a prototype in Django, because its built-in support for almost everything makes it easy to throw together a proof-of-concept. Django’s batteries included approach bypasses a lot of common development pain points. Then when you've tested an idea and know what you want to do and what you need, you can switch to Flask and customize your environment with exactly the components and support your application needs.
There are lots of production applications built in Flask for exactly that reason; they don't need everything Django has. If all you're building is a simple API service that responds to requests, you may not need fancy features like authentication and session management.
When considering Django vs Flask, the best way to make the choice is by understanding what your project's end-state is and asking what will your project need a year from now, not what does it need today.
Despite all the emphasis on rapid delivery, writing code is really about building applications for the long-term. Knowing what you want your application's future to look like can help you choose the right framework today.