What are some of your best Django practices?

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?”

  1. 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)

    Reply

Leave a Comment