Just recently I started to learn Python and Django to build a web application. I am starting to really enjoy how Django is structured as a web framework, and definitely want to learn more.
However, I am starting to see that my code is starting to look very messy and the structure started to look very clunky, especially my templates.
So I am starting to try to keep as much logic out of my templates and instead be in the view (after reading this article: http://shibel.blot.im/django-keeping-logic-out-of-templates-and-views).
So I am curious, what are some of your best, recommended, or favorite Django (or Django Rest Framework) practices that you like to share.
4 thoughts on “What are some of your best Django practices?”
Can I add to the question and throw in DRF practices?
> So I am starting to try to keep as much logic out of my templates and instead be in the view
I was going to say I always thought the consensus was models > views > template for logic and then I clicked the article and it does advocate that as well. You also might want to check [this](https://stackoverflow.com/questions/12578908/separation-of-business-logic-and-data-access-in-django) out.
If there is a lot of logic for a view, consider moving most of it to a utily or business logic file..
Hey, author of the linked article in the OP. The article actually advises not to keep too much logic inside views:
> So, takeaway: keep logic out of templates almost at all costs, try to have as little of it as possible in your views. If you want something quick, a model method may suffice, but prefer model managers.
There are actually pretty recent school of thoughts that advise for “thin models” if your app is really big. I’ll probably write about that on the blog as well.
I’m working on a huge best practices article as well, but here’s a quick one off the top of my head, nothing novel or groundbreaking:
If you have an app which utilizes a DB, download dj debug toolbar, examine your database queries and see how you cane make them more efficient (with `select_related`, `prefetch_related`, and `Prefetch` objects)