pebox11
pebox11

Reputation: 3748

Where to place external python scripts in a Django project?

Where someone should place any external python script for her Django project? What is the more appropriate location (if any)? Shall she create a folder in the main Django project and put it there and add this to the python path or is there a better way to deal with this issue? The reason for external scripts is not to overload the views with code that can be better organized in script files and can serve more than one views.

Upvotes: 11

Views: 12979

Answers (3)

gamer
gamer

Reputation: 5863

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

Upvotes: 23

CleverLikeAnOx
CleverLikeAnOx

Reputation: 1496

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.

Upvotes: 1

gsb-eng
gsb-eng

Reputation: 1209

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/

Upvotes: 0

Related Questions