Best Practices for Structuring Django Projects?

Hi Django community!

I’ve been developing a Django project for some time now, and since it’s expanding, I feel that my project structure is becoming unorganized. I’ve gone through various strategies, such as utilizing a src/ directory, organizing apps into small modular pieces, and keeping settings organized using environment variables.

I’d be interested in hearing from seasoned Django developers:

How do you organize your Django projects to be maintainable and scalable?

Do you have any particular folder conventions or best practices for remaining tidy?

What are the best practices for structuring a Django project that includes a Machine Learning Tutorial module?

Would greatly appreciate any input, examples, or even pointers to good resources! Appreciate it in advance. ???

Looking forward to hearing from you all.
Regards
boom-hue

Welcome @boom-Hue !

This is a topic that has been discussed frequently here. You can search this forum and find many topics talking about this.

For my opinions on this, you can get started with the topics at:

1 Like

Hey @boom-Hue.

Structuring a Django projects always leaves one with doubts. The possibilities are endless and we keep wondering if we are making the right choices. The good news:

The right way™️ does not exist. Each django project is unique and should be considered in its own. And remember, you can always move things around as needed…

Obviously, if you shoved everything in the same app, it would byte you in the long run…

Here’s what I do:

Every thing that seems separate goes into separate apps. I prefer small apps rather than big ones. Imagine your app is a social network. You could probably have on app for the users (profiles, user management, following mechanism), another for the social aspect of it (posting messages, or photos). You could even have some code to send a weekly newsletter, but stored in a separate app.

Internally, I keep things simple and as close as possible to what Django give you.This is because if you later work with other, it will be easier for them to ramp up.

Apart from splitting into separate small apps as mentioned above, I keep my models.py, views.py and so on as is. Once they get too big (if they get too big…), I might split the file into a folder (like having views/init.py, views/user_account.py, views/user_follow.py, …).

Only thing I do differently is try to not shove too much into the models and create a separate services.py layer to handle multi model logic (warning: models should never import services).

Thanks the gist of it.

I disagree with this statement because I think it’s making a generalization that isn’t necessarily universally true.

Not all projects need (or benefit) from being divided into multiple apps, and, the process of converting a single-app project to a multi-app project is not necessarily difficult or time consuming.

I’ve got many projects that have been running for years - quite happily as single-app structures.

You are right. I was thinking more about big apps. Obviously for small-to-medium or single-focus webapps, a single django app is enough.

Also a generalisation that isn’t necessarily true.

But anyway, here’s the important bit for me…