s_m_lima
s_m_lima

Reputation: 107

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

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.

Upvotes: 3

Views: 234

Answers (3)

Azharuddin Syed
Azharuddin Syed

Reputation: 43

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.

Upvotes: 1

Conor Romano
Conor Romano

Reputation: 150

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

Upvotes: 1

nigel222
nigel222

Reputation: 8192

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

Upvotes: 2

Related Questions