Crowdbotics Logo

Customers arrow

Don’t take our word for it, see what our customers have to say.

About Us arrow

We are on a mission to radically transform the software development lifecycle.

Home Blog ...

Django

When To Use Django (And When Not To)

Choosing a language and framework because you used it on your last project — or because you are more familiar with it — is not the way to go. Before starting a new software project, you want to evaluate which language and framework is the best fit for your desired outcomes. What matters most to you? Security, rapid development, scalability, versatility, support? It’s better to make an informed choice before you begin your build rather than regretting it later (or worse “hacking” stuff into the project down the road because the stack you used doesn’t support it properly).

6 October 2021

by Kalpit Jain

Helping developers evaluate whether Django is the right framework for their next project. Chances are, it is.

After years of experience with different technologies (including both mobile & web development), I believe Django offers a complete suite of features which no other web framework offer.

I know that’s a big claim. Let me back this up.

“Django powers many of the Web’s most-used sites, like Instagram and Pinterest, even Facebook uses Django for its many behind-the-scenes utilities. Django came from publishing, so it’s no surprise that sites like The Washington Post and Smithsonian Magazine use Django.” — Amit Ashwini, VP of Marketing @ Zibtek

High-level: When to use Django

If you can check even a few of the statements below (without strongly disagreeing with any), chances are that Django is good for your project.

In addition to the above points, your own (or your team’s) skill-set should be considered as well.

If you’re already a web developer with knowledge of “how the web works”, then working with Django will be comparatively seamless. You only need to understand how Django is structured (and some other things, of course) and you’re good to go.

Websites that Rely on Django Framework

Django has been around for over a decade now. During this period lot of top websites have used it in production, some notable examples are:

Are you still wondering if it’ll be worth to invest your precious hours to get up to speed with Django? First, consider reasons why Django may NOT be a fit for your project:

When not to use Django

If the above does not apply to your project, chances are Django might be a fit.

Reasons To Use Django

Django Framework is built with Python:

I know you’re familiar with this fact.

I’m just taking this opportunity to highlight some of the key benefits of Django which it inherited from Python. I’ll keep it short.

“Program development using Python is 5–10 times faster than using C/C++, and 3–5 times faster than using Java.”. — source

Django has “Batteries Included”:

“Batteries included” means that Django comes with most of the libraries and tools required for common use cases, out of the box. Django ORM, Middlewares, Authentication, HTTP libraries, Multi-site support, i18n, Django Admin, template engine etc. are some of the “batteries”. No other framework that I know of offers this much support out of the box.

Some people prefer to put this under “Cons”, while others put it under “Pro”. Each side has its reasons, and I agree with both sides to a deg

It is a con because it makes the framework a monolith.

My argument is that if you require those features which make it a monolith, you’re going to use some other library (or write your own) anyway.

Why not use something which is baked right in, battle tested, powers some of the largest websites on the planet, actively developed, and supported by the community?

If you don’t require most of the features offered by Django, you should consider using a micro framework instead.

Don’t reinvent the wheel, remember? Spend your time on the stuff that matters, let Django handle the rest.

Django Admin

Though I mentioned it in previous section, it needs more attention. Many frameworks, like Laravel, Yii, etc. have attempted to make working with admin panel easier. I’ve developed many projects in various frameworks, but none of them are even remotely closer to Django framework in terms of working with Admin panel.

Some people argue that Django Admin isn’t flexible enough, and customizing any part of it requires a lot of effort. I used to agree with this statement during my early days with Django, but eventually, as I got familiar with the framework, I found out that this is incorrect. Yes, there’s a learning curve — but it’s worth every second.

Django Admin is actually very well structured. In some of my projects I’ve used Django admin as-is, and in others I’ve completely replaced it with custom templates developed from scratch. In any case, it did not take longer than it would have if I had developed it with any other framework I knew.

The best part? You get permissions and authentication module out of the box. If built from scratch, this could take weeks (or days) at least.

DRY Principle (Don’t Repeat Yourself)

I’ve seen many frameworks boast about being “DRY” compliant. I’ve worked with many of them, but none get it right.

Most frameworks, unfortunately, don’t care enough to really follow the “DRY” principle. In my opinion, if you’re building an app which you’ll be updating regularly (most apps today), you must follow DRY to avoid issues.

In Laravel, for instance, you’re required to write validations for each route separately. This is the case with most other frameworks. To make your code DRY compliant, you need to make efforts. It’s hard to keep check especially when you’re working in a team.

Django framework, on the other hand, is designed in such a way that you have to go out of your way to violate DRY principle.

That’s the way it should be, right? Need an example?

Here’s how validations & database migrations work in Django:

  1. Create a Model class with required fields. Specify any extra validations and constraints as needed.
  2. Migrations are generated with just one CLI command: `python manage.py makemigrations`.
  3. Database is updated with the changes with just one CLI command: `python manage.py migrate`.
  4. The validations and constraints are automatically checked on each CRUD operation — be it in Django Admin or Django REST Framework. You don’t need to write validations ever again.
  5. The same model class is used to generate Django Admin CRUD views. No custom HTML/CSS required.

If you compare this with any other framework, I don’t think it’ll be able do all this with just the following lines of code.

class Employee(models.Model):name = models.CharField(max_length=127)email = models.EmailField(null=True, blank=True)created_at = models.DateTimeField(blank=True, null=True, auto_now_add=True)updated_at = models.DateTimeField(blank=True, null=True, auto_now=True)

It’s not just about “not repeating yourself”. It helps you avoid bugs in the future. We all have been in situations when you changed something at some place, but forgot to do that at other place and found it out only after many customers ran into the issue.

In Django, referring to the above code, if you ever need to change a field’s `max_length` to something else — just do it here. It’ll automatically be applied to all routes’ validation & database.

Django Framework ORM

Django provides a fully functional ORM right out of the box.

I’ve worked with many ORMs in different technologies, including Eloquent, greenDAO, Yii AR, etc. All of them handle basic queries pretty well, but at some point I always find myself writing raw queries because the ORM fails to address the use-case.

I’ve not run into such a case yet with Django ORM. It’s so well built that it makes you forget that you’re actually working with database queries. That’s how an ORM should be. Below are some examples of Django ORM:

# gets top 5 results where rank = 10 and age <= 30top_young_employees = Employee.objects.filter(rank=10, age__lte=30)[:5]
# inserts a record with specified valuesemployee = Employee.objects.create(name=’John Doe’, age=35, country=’IN’)
# prints field valueprint(employee.name)

Rapid Development

This is something almost all web frameworks shout out loud, and they’re probably right with their own definition of “rapid”.

With Django though, you can get stuff done ridiculously fast. You just saw how rapidly we were able to define admin UI, database table, and validations above.

That was just the tip of the iceberg.

Although it’s not particularly a feature by itself, it’s actually a by-product of following DRY, ORM, template engine, and “batteries included” philosophy.

Django Framework Security

Let’s face it, developers are sometimes lazy. At least I am. More often than not, I procrastinate doing some tasks which aren’t crucial for development. These tasks usually open doors for security vulnerabilities.

The part I like most about Django is that it doesn’t compromise on security to offer rapid development. Security features are enabled by default so it doesn’t matter if you’re lazy.

Open Source, Well Documented, Huge Community, and More

Being open source and insanely popular, Django has created a helpful community. I’m assuming you’re aware of the advantages of open source software. Django has the same advantages.

Django’s official documentation is more than enough for developers. You can easily find solutions if you get stuck. On top of that, Stack Overflow is flooded with questions & answers related to Django.

So far we’ve seen that Django created a lot of libraries of its own, so it might surprise you that it didn’t create any library for testing. It doesn’t mean that Django framework doesn’t support testing — it does. They have a complete section dedicated to testing in docs. Following the principle “Don’t reinvent the wheel”, it’d be pointless to develop a testing library when Python itself provides a great one itself. Django just plays nice with it. It also works well with other popular third-party libraries like pytest.

I’ve tried my best to address the issues I faced while working with other frameworks in contrast with Django. After working with Yii, CodeIgniter, WordPress, CS-Cart, Laravel, etc. I’ve found Django to be a lot better than any of them.

It’s just my opinion though.

If you’re into statistics, here’s Stack Overflow’s yearly survey showing Django amongst the top used & loved frameworks:

If you’re as paranoid as I am, you probably spend more than enough time on application architecture and code structure.

Apart from my experience with PHP framework mentioned above, I’ve also developed Android apps in Java, and front-end apps with React.js. In all of them, I’ve spent more than required time refactoring codebase, finding the best architecture, running into scalability issues after a few months, then refactoring again.

I recently moved a production app running for over a year from Laravel to Django. I was able to deploy the new codebase within 10 days and very few lines of code (re: reduced complexity)! Had it been the other way around, it would have taken more than a month for sure.

If you try to directly compare other frameworks with Django, you won’t get anywhere.

Performance benchmarks may show a Java framework faster than Django. You might be familiar with PHP so it might take you longer to develop in Django app as compared to a PHP framework you’re familiar with. For a really simple app, you might find setting up Django a bit tedious as compared to a single script file. Different polls might show different results based on the audience they reached.

It’s not just about frameworks of other technologies though. Even if you’re familiar with Python, you might find Flask micro-framework more popular or suggested more than Django. It’ll make you wonder which one should you go for.

My advice? Don’t compare these two.

Django is a full-fledged framework with “batteries included”, Flask on the other hand is a micro-framework . Define your use-case. Identify your skill-set. Then, choose one.

Conclusion

In my opinion, Django is the perfect balance between performance, architecture, development effort, security and scalability.

If you’re starting a software project from scratch, I’d personally recommend Django.

I’ve tried my best to pen down my experience with Django in contrast with other web frameworks I’ve worked with.

                                                                 .    .    .

If you find any errors, or feel any need for improvement, let me know in your comments below.

Have you worked with Django? Let me know your experience in comments.

                                                                .    .    .

Thanks to William Wickey for editing.