[Django]-Django – Where do you store non-django .py files in your app?

1πŸ‘

βœ…

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
πŸ‘€Conor Romano

2πŸ‘

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

πŸ‘€nigel222

1πŸ‘

I personally like keeping my views file clean as well i.e. they only contain views and nothing else.

As for the other files it depends on personal taste, in your case I recommend creating a directory called scrapers or web_scrapers for storing the scraping files. You should name them to the specific purpose that they serve like football_scores_scraper.py or stackoverflow_scrapper.py. And for the file that contains utility functions keep it inside the same directory as views.py but name it appropriately depending on its usage like tax_utils.py or if it contains different functionality just name it as utils.py.

Leave a comment