Question

I'm writing a simple Django app and I'm having trouble finding a place for general purpose Python functions (not views). Some of the views in views.py are getting chubby and I want to outsource some calculations to a couple of general purpose functions.

Should I create some outside module for that, or it is perfectly normal to put them in views.py. What is the general policy about mixing Django-related and generic Python code? I've been thought that generally Django doesn't care, but I can't help cringing when I do that.

Was it helpful?

Solution

Lots of people create a utilities.py and put general purposes functions there.

Then, in your views.py:

from .utilities import function_name

If you want to use them across apps, it might be best to put them in your main project folder (I.e. where settings.py is) and import from there. Nothing stopping you from importing from one app to another, however. Just call it correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top