Python is everywhere!
The place where it is really starting to grow and dominate is the web.
Thanks to the development of the web framework Django, Python has become a big competitor.
But since it’s release, a few other players have entered the market.
Most notably Flask, and Pyramid.

Django is often described as a web framework with batteries included.
And Flask is described as the micro web framework.

Now, I have used both, and in all honesty it’s very clear that Django is the most popular.
But sometimes I really have to ask myself why.
My personal preference lies with Flask over Django.
And let me explain why.

A lot of times, when a framework becomes popular, developers who were at home with things like PHP will learn about the frameworks. In this case, Django.
Whilst in essence Django is a Python framework, it does things slightly differently, and knowledge about Django isn’t always about needing to know much Python. Flask on the other hand acts more like an add-on to Python than it does as a stand alone framework.
I have often had a bit of a debate about Django vs Flask with ‘Djangonauts’ (yeah, I know, I feel sorry for them too).
And there seems to be a lot of misunderstanding.

The following is what a Django developer will tell you why you should choose Django over Flask:

  • Django has an admin page, which Flask doesn’t
  • Django is a batteries included framework
  • With Django you can easily make large apps which fit together, Flask is for small projects.
  • Django has an amazing ORM which Flask doesn’t

Well, that sounds a very strong argument. If it was entirely true obviously.

It is true that Django has an admin page which can be turned on and is ready to go.
Whilst Flask doesn’t have that built in, thanks to Flask-Superadmin which is installable using Pip, you have the same functionality with Flask without writing lots of code. Sure, you will need to add it yourself, but I’ll explain later why I think it’s worth it.
Django is indeed an all batteries included framework and Flask is not, but that isn’t always amazing. If you are writing a simple blog, you may be working with a huge framework which isn’t necessary.

The third argument in my opinion (and perhaps mine alone) shows a lack of understanding of Python.
The notion that Flask cannot build different apps linked together means you do not understand how to make modules in Python. Make a new folder, make an empty __init__.py file, and your app file and you have an independant app.
Then call that app from the root directory in a main python file and it’s done. There is basically nothing stopping you from building a large scale project with multiple apps in Flask. The only difference is that in Flask you make those files by hand, and in Django it is automatically generated.

And the Django ORM is great, there is no denying that. But, there is an almost identical ORM for Flask which many are familiar with called SQLAlchemy, which can be used with the package Flask-SQLAlchemy.

An often not mentioned argument is the long term maintenance of a project.
Thanks to the complexity of Django, long term projects are a nightmare.
Dependencies, the not overly Pythonic way of programming all end up meaning with upscaling and growth, you end up having to change code all over the place.
Because in essence Flask is very much a Python project, if you program it well, it means long term projects are easily maintainable.

But for me the biggest plague is the routing of URL’s.
Django uses Regular Expressions. Which, whilst useful for many things, is not the ideal way to be routing.
And on top of that, the routes are in different files to the actual code being called.
So it works like this:

A user goes to ‘/’…this is looked for in the urls file until found which refers to a model…so it goes to the model it referred to and runs the code.
Doesn’t seem too bad, but changing one route, or adding it means you have to change at least two files.

Flask does it different, it firstly doesn’t use Regular Expressions. It simply uses a line like this:
@app.route(‘/’)

Seems simple right? There in the brackets is the route.
But even better is that the code that is to be run for that route is directly underneath it.
So this may be a complete route and function:

@app.route(‘/’)
def home_page():
render_template(‘home.html’)

That’s it.
All it does is check which route is being requested, goes to the function directly beneath it, and with the render_template function it gives back the home.html page for the user to see.

And a project can look complicated, or be a single .py file.

So in my opinion, Django is overrated and bloated. Try Flask!

Advertisements