10 Libraries for Django projects

Django remains and will continue to remain, the most popular Python framework for web applications. It continues to grow and evolve, and there are a huge amount of modules and libraries that not just work well with Django, but are essential to building apps for the current day.
Here are my choices of libraries to use. A lot of you will disagree with some on them, but I not only see these as great choices, but the best when it comes to looking at your projects with the future in mind
Python Dotenv
There are tons of secrets and settings that should never be in the settings.py file and of course you can just use the ‘os’ module to get them from environmental variables. This is often the case for deploying to selected types of cloud services. However Dotenv still provides the simplest way to set-up variables on each server instead of inside the settings file. Its really simple and often much more flexible than other options.
For me this is the first library I install, so no matter what or where I deploy, my settings file is secured right from the start.
Django Ninja
I used DRF for many years, but I fell head over heals in love the first time I saw Django Ninja. The library was designed to bring in some of the best features of FastAPI into the Django world, as well as being an API framework. Dependency injections, Custom authentication, data validation and a lot more come with this lib. The creation of API’s is very much streamlined and fast to do. It has hooks into the orm, but its pretty much standalone. This in itself makes me like it even more. Just a warning through. Django Ninja is VERY much on the FastAPI style of things and not the Django style. There is a learning curve but one that is really worth it.
Pydantic
I have a confession to make. I hate Django Forms. it just seems to get in my way when I use it and never really meets my expectations. The thing is, I want my forms to be validated properly and to be able to pass that data back to a template, but I don’t want it to be in control of how my form is rendered.
Pydantic extends the idea of Python Dataclasses, thus follows the route that Python is taking, rather than the route Django wants to take. It allows for layers of validation to be done against the form data, both at default levels using python typing and by adding in your own validation routines.
For me Django Forms are the past and Pydantic is the future. Its also easy to create a wrapper class where you can state the Pydantic model to use for validation, along with the form data.
Python Social Auth
When I first started Django AllAuth was the package to use, but I think its had its day. Python Social Auth comes with its own Django integration modules and is really simple to set-up. There are no pre-defined templates or forms to overload, so you create your own using the easy to include url references. It doesn’t do all the email validation etc, but I’m a KISS type person and if I want those, I will add them in. Its not like its difficult to do.
Pika or aio-pika
Either put Celery out of your mind or dump it now. Using RabbitMQ with Pika or aio-pika is so easy, you will question why Celery takes so much to setup. I have written articles on this already, so no need to go down this route too much. I will just say that Celery does things the Django way, while Pika does things the Python way. In this case the Python way gives a Huge amount of extra options and a simplicity that you will find unexpected.
HTMX
So why do I have a JS library in my list. Well HTMX with Django is really the future. It allows you to build reactive front ends without the need for a HUGE frontend framework and two completely different environments. Plus its so dam easy. It really feels like HTMX was written to be part of Django in how it fits into your projects. It changes how your templates are designed and adds so many options that you will wonder how you did without it.
Django Templates Partial
This is another tiny addition to your project but one you will find useful especially with the use of HTMX. Basically you can put multiple templates into the same file, marking them as partials with a reference. Then when you render your template, you can reference using the #. ‘my_template.html#my_partial1’ This drastically cuts down the number of tiny html files you might have all over the place.
Django Cotton
Front end programmers go on all the time about components and Cotton is the simplest and easiest way to do this from Django instead of the front end. Some other options offer a Jinja2 Macro like option, but Cotton’s power extends beyond those and allows for properly embedded components that can be used to make your html readable. Its also pretty easy to use, with the components automatically registered and no need to do any loads inside your html. It also used html tags and not the {%%} format of most django tags, giving your html a much cleaner look and feel.
On a last word on this, I did start using a lot more custom tags on a project with HTMX and found it changed the way I wrote the templates. Cotton takes this to the next level, simplifying everything while making the html look cleaner
Faker
Most Django coders will mention loading Factory-boy for creating test data, but with continuous use, I’ve found the Python Faker library on its own, is far more useful. I think this is down to not wanting my modules to be tightly woven into the Django framework and the ORM, thus giving me more flexibility. There are also a lot of use cases for Faker outside of unit testing. Don’t get me wrong, if you want Factory-boy then go ahead, you get Faker with it anyway, its just I found myself using Faker and not Factory-boy.
Python Systemd
Ok this one is really a personal choice. It won’t work inside of a standard docker container and systemd is not installed, but outside of that its Fab. Python’s logging system is great, so I am not saying throw it all away, but using the journal and logging through systemd, just adds a whole extra dimension to your log data. The main thing is adding all sorts of additional parameters or fields to each message.
On top of that you can kinda check on other processes etc more easily than via ‘os’ issued commands.
One last thing is that Python Systemd also allows you to read the logs using the full journalctl sets of commands. Thus its a great way to display log messages inside your app.
Final words
I really do like Django, but some of its batteries are old and need replacing. The libraries I’ve recommended here are not the most ‘Djangoic’ type ones, but are more Pythonic. I think they bring new life to Django and will make your Django project better. It also stomps on the React FE/Django BE stack. That just has to go
