0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Why you're doing relative import of .utilities explicitly here?
Just general Django best practices. This way you won't need to change it if you change the app name or directory structure. For most purposes, it's probably fine to just import from utilities but either way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.