[Solved]-Where to place external python scripts in a Django project?

23👍

Create a folder, say utils, and make it a module by creating __init__.py inside it.
Now create any script under this folder. Let’s say you have a file called utils.py that contains some of your python code and you want to import it.

Wherever you want to import then import your python script like

from utils.utils import YourClassOrFunction
👤gamer

1👍

You can have additional files that have generic code in their own files and just import them when you need them for your views, or whatever. If they are only going to be used in one app, then just put them in that apps folder. If they are more generic then you can put them in a file or folder that isn’t part of any particular app.

Django does some magic with files and folders it expects to exist, but you can put other files wherever you want it works like any other Python project.

0👍

Adding project path should be done at your manage.py, but if you wanted to do something before your request reached views.py you can use middlewares. https://docs.djangoproject.com/en/1.8/topics/http/middleware/

Leave a comment