Diving (back) into Python - Django
04/09/2024
As part of a couple of personal projects I've been needing to write some code for a web application outside of the work context, so I've been able to make some different choices than I'd normally make and have been working under some different constraints:
- Speed of delivery is important and needs to support quick working
- I need to be able to drop and resume development as quickly as possible, as I've only been able to work on this project in short bursts - even more important at the moment as life has been very busy recently and there's been less time than previously to work on it. Ecosystem has been quite important
- Needs to be something I'm at least familiar with, as I wanted to concentrate on building the thing rather than learning on the job
This forced me a little bit outside my comfort zone when choosing which stack to use.
Technology Choices
The choices were really: .NET (probably F#), a full-stack JS framework or something in Python, the languages I'm most familiar with. I also wanted to stay outside the Azure space to avoid any overlap with my day-job.
I narrowed down the options to:
- F# with Giraffe and Dapper, plus Fable on the frontend - but discounted this eventually as I hadn't done much with Giraffe or Dapper at all, so felt it was a bit too much of a learning curve. The ecosystem of packages is small for F# only packages, but you also have wider .NET libraries to use. I'd like to spend a bit more time on this stack in the future, but discounted it as I wanted to spend as little time as possible getting something running end-to-end with a frontend and backend working together.
- Next.JS - as it's React based there's quite a bit of overlap with the day job, but I needed things for the backend too. I tried a couple of different ORMs and Drizzle was the one that I liked most. Next does look nice and handles things like auth with its own integrations. I was also using it as a static site generator for this blog. As with most things JS though, I felt like I had to assemble a lot of the stack by hand.
- C# and Asp.Net/Blazor - I've done a couple of experiments with this and have since been involved with a couple of things in work that have used this stack. I discounted it at the time but again, I'd actually like to spend more time with it myself.
- Django - I'd used this briefly when it came out, and looking at the docs it felt very familiar still. I did a couple of spikes on it and the ORM was easy to use. The ecosystem of libraries around it is pretty comprehensive, so I settled on this as the choice.
First thoughts
Python was my main language from the end of university, but I hadn't programmed in it to much of an extent since around 2016 or so, so I needed to revisit the current state of the art. The great thing about Python is that it hasn't changed that much, so even though I felt like a bit of a caveman, it felt familiar. There are a few things that I'm still coming up to speed with like tooling, but I was able to make some fairly fast progress.
Django has been quite similar - they got so much right at the start that the fundamentals haven't changed that much since the beginning, but the current features have mainly improved things.
The admin system is the main thing that's saved me time when building - having the ability to scaffold out data for models allows CRUD operations to be done as needed in the frontend. It saves so much time that I'm amazed that other frameworks haven't followed suit.
The ecosystem of packages is wide, but there are mainly one main third-party packages that does a particular function and there mainly aren't several overlapping ones (like the JavaScript ecosystem).
I ended up adding in JS in the form of React in the frontend as well in the interests of my speed of build, although getting this wired up correctly was frustratingly slow! I used normal Django templates for many things, and then used React for interactive pages in a 'MPA' style. I'm pretty happy with the results but am looking at things like HTMX with a curious eye...
Libraries
In addition to the out of the box functionality, I've pulled in the following packages, which I've been generally pleased with:
- django-tailwind (for backend CSS)
- django-ninja (for API creation). I really love how easy this library has been to use and how little boilerplate it uses.
- allauth - for social logins. I tried a couple of other libraries for auth with varying levels of success.
- django-webpack-loader (for loading multiple React bundles - I tried and failed to get django-vite working for this, something to retry at some point)
- Flowbite and the React bindings to get the same UI look and feel between frontend in React and Django templates
Some backend tasks were written as django management commands, I haven't found a good scheduling library yet.
Things that I need to spend more time on
All in all, I got things up and running to a certain point, but there's work to do before finishing. I want to understand Allauth better as I just got the basics working and authentication is always a big requirement for modern apps. I also spent far too much time getting the django webpack integration running alongside tailwind, and I really want to see if I can transition to django-vite in the future.
Resources I used Aside from Copilot & ChatGPT, plus the readme's for the
libraries above, I found the following articles useful:
- Django and Webpack Series
- Building your Django and React project using Django Webpack Loader and Webpack Bundle Tracker
- Allauth documentation
- Flowbite & Django setup
Current thoughts
All in all, I'm relatively happy with the choice of stack at the moment and I think it's got me up and running relatively quickly. I'm sure I'll add libraries as I go, but my experience thus far has been quite good.