Django: an Include-Your-Own-Batteries Framework

5 min read Original article ↗

Sławomir Górawski

Or, “My wishlist for Django 6.0.”

Press enter or click to view image in full size

Day 0

I heard that Django is a great web framework. Even in 2024, when I want something like a REST API with a React frontend rather than an all-in-one server-side solution, it should help a lot. With Django, I get a great ORM, database migrations, auth stuff like password hashing, sessions etc., and the famous admin panel — all in an already-working, opinionated package.

Another benefit is access to a wide range of packages for everything that I can just add to my INSTALLED_APPS, and they’ll work. I’m sure it’ll come in handy one day, but I’m just starting my new project, so I guess I don’t need any yet? After all, I have a whole “batteries included” framework at my fingertips, it’s gonna be a breeze. Define some models, populate some records in the database, there is even a command to create an admin user, all very slick. Seems like most of the backend is already taken care of and I can just focus on tinkering with my React components.

Day 1

I have a working frontend on localhost, time to fetch some data from the API. How do I do that in Django? Apparently, everyone is using this thing called Django REST Framework. Hmm, I thought Django was the framework, why do I need two frameworks? Well, I guess I can think about that later, let’s just install it and get going.

I should write something to handle my requests, apparently I should decorate a function with an @api_view for that. Or I can inherit from an APIView class, I guess that’s better. Hmm, why does my class lack some methods that I’ve seen in that StackOverflow snippet? Oh, that was a GenericAPIView, I see. Ok, now that I can list all /posts it would make sense to handle GET /posts/:id in the same class, can I do that? Not until I replace my GenericAPIView with a ViewSet! Ok, how about converting my models to JSON? (Goes down the Serializers rabbit hole.)

Day 4

Finally, I got the API working, time to test it from my React app. CORS error! Googling… Apparently, if the API is served from a different location (host/port) than the requesting website, it needs CORS headers to let the browser know that requests are allowed. And the community seems to agree that the django-cors-headers package is the solution to that. I should just install it, I guess. ¯\(ツ)

Day 5

Ok, the requests are working now, I can list my posts using the API. Would be nice to add some filtering, maybe by author or publication date. I should check the REST Framework documentation, surely it can do that. “To use DjangoFilterBackend, first install django-filter.” Oh.

Day 15

Moving on, a couple features later, now I think it would be a good time to add notifications about new posts! Polling the server every second seems inelegant, everyone is using WebSockets now. Does Django support those? Googling… Well, not out of the box, but there is a separate package for that, Django Channels. Yay.

Get Sławomir Górawski’s stories in your inbox

Join Medium for free to get updates from this writer.

Remember me for faster sign in

At least setting it up is very straightforward, all I gotta do is switch from WSGI to ASGI, replace the default development server with one that doesn’t reload correctly after errors, and dive into the world of asynchronous Python with asyncio, event loops, coroutines and stuff. Easy.

Day 31

After a while, the time has come to offload some features to background workers — I can’t just block while handling a request every time I need to connect to some external service. No Django support for that, the default in this space seems to be Celery. Hmm, that surely does look like a heavy dependency. And I’ll need to set up RabbitMQ or something like it, that sounds hard. Can I at least use the database that I already have as my queue for now? Checks docs… Nope. Oh well.

Day 70

Is it just me, or do I need a lot of extra batteries to get some basic stuff done with this batteries-included framework?

Press enter or click to view image in full size

Afterword

Ok, ok, I like Django a lot. It jumpstarted my career as a software engineer and made my first jobs enjoyable, and I’m very grateful for that. The REST Framework is a very powerful tool that enables one to quickly bang out production-ready APIs with a lot of important stuff taken care of; it may just be a bit hard to get at the first encounter. Thanks to the creators and maintainers of all the other mentioned packages for making it possible to quickly solve the problems with a pip install and a 5 min setup.

It’s just that after spending some time with Laravel I wish that my OG framework of choice would make it a bit more straightforward to do what most (citation needed?) of us are actually doing. And I don’t think I’m wrong when I say that in 2024 more projects need a REST API, CORS headers and WebSockets than a built-in PostGIS support or yet another a-prefixed method that uses a thread pool anyway.

Stackademic 🎓

Thank you for reading until the end. Before you go: