-
Image via Wikipedia
When I started coding for our latest project at Preona a bit of an epiphany happened. I suddenly got django. Every pattern that used to feel a bit strange and I may have fought a little, suddenly became obvious and simple. Suddenly out of the blue my whole codebase is so marvelously organised and separated into files it brings a tear to my eye.
In the old days the average Django App in my project had a structure a file structure a bit like so:
- models.py
- views.py
- tests.py
- RandomActualWorkLogic.py
- AnotherRandomOfWork.py
- ...
Sure this works quite alright, but it feels a little weird and after way too much work you suddenly realise that instead of the clean design you envisioned as you started out, you're left with a crow's nest of interdependancy, circular crappery and your project is in pretty miserable shape.
This point was really driven home when I figured two different django projects should share some code. Extrapolating a clean interface and making stuff work ... was a nightmare. I actually gave up after a week and decided to start afresh.
And boy, was that the best decision ever!
Better application structure
The first step was to take up a better way to organise my codebase inside applications. It goes something like this:
- models.py
- managers.py
- views.py
- tests.py
- forms.py
- decorators.py
- processing.py -- (this is for certain special views)
- urls.py
- settings.py
The first result of this organisation is that now everything has its own place. There are no StrangeWorker.py crappy things anymore. All of those reside in a special 'application' just for those called a _lib. _Whenever you are making something and are looking for a place to put it -> the file is an obvious pick!
Another great result is that it's now much easier to keep applications contained within themselves. Because both its specific url config and its specific settings config are contained within the application you can easily switch it around between projects or even make it a standalone thingy that projects can simply include.
Also because the file structure makes sense now and all those WeirdoWorker.py are separated elsewhere as proper libraries, there is less of a temptation to create mixed and confusing dependancies all over the place.
Conclusion
That's it for django application structure. If you've got any better ideas do mention them on twitter or via the comments below. If not, that's alright :P
Either way, join me in the next installment of Django Protip when I'll be talking about using "Forms, forms for everything"
Continue reading about Django protip #1: A better App structure
Semantically similar articles hand-picked by GPT-4
- Django protip #2: Forms are awesome
- Symfony and the scary world of PHP
- Make mistakes easy to fix
- DjangoCamp was bangin'! (before unseen epic last slide of my talk)
- Blackbox testing node.js apps
Learned something new?
Read more Software Engineering Lessons from Production
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Software Engineering Lessons from Production
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️