Where in django tree should I add python utility files?

I have a number of files with python code called by views to do necessary computation,
that I would prefer out of views to keep them clean.
Is it in the “standard” django tree a place where these files are best kept?
Thank you!

Django-itself doesn’t have a strict opinion on the placement of utilities/common code.
You can place them wherever you want.

[Opinion]
Normally, what i do is:

  • Keep the code where is it used, if code from another file (inside the same app) uses it, i will:
  • Create a new file under the same app, normally this is called utils, or {something}_utils depending on what that code does, now if code from outside of that app is using that code, then
  • I will place this on my project root directory, the one that have settings.py, and now if i will reuse this code outside a project, then i have some more options.
  • I can just copy/paste this code to my other project, or:
  • Create a internal python package that have the utilities that i use.

The idea is simple, and i apply this to most of the things on software development: start small, then grow as you need.

1 Like

I chose your solution n.2, seems perfectly logical. Thank you!