Home > Enterprise >  Django - Where do you store non-django .py files in your app?
Django - Where do you store non-django .py files in your app?

Time:12-24

I have three python files (two that scrape data and 1 with my functions) I'm using in my app. Each file is about 150 lines, I don't want to include this code in views.py to keep my views as clean and readable as possible. Is there a best practice to keep things tidy? A separate folder inside the app with all external non-django .py files (similar to 'Templates' for .html or 'Static' for .css)? Any suggestions are appreciated.

A Google search didn't yield any results.

CodePudding user response:

I have an app called utils into which I put everything that's likely to be shared across multiple apps. So,

from utils.foo import bar, baz

Making it an app might be overkill if you don't have any models / DB tables that you want to disconnect from any particular app. An alternative would be (I think) to just create a folder under your project root and turn it into a Python module by creating an empty __init__.py

CodePudding user response:

You could create a separate app-- it depends on how sectioned off you want things. I prefer to just include the non-django .py files in the app that you expect to use them in the most. And then import the relevant functions in views.py from there.

Sample of how the import would look in views.py for a function (called function) from a non-django .py file called operations.py located in the same app as the view:

from .operations import function
  • Related