Vikrant
Vikrant

Reputation: 101

How do I use external python utility files in a Django project?

Suppose the name of my Django project is 'django_project' which contains an app named 'main', and I have created a directory called 'utilities_dir' that contains a few python files (like 'utility_1.py', 'utility_2.py' et-cetera) having the functions I want to use. Where do I place the 'utilities_dir' and how do I use those files in, say, my 'views.py' file in the 'main' app directory? Thanks!

Upvotes: 0

Views: 3050

Answers (2)

Archit Verma
Archit Verma

Reputation: 1909

If I understand your question correctly you want to import some files from a folder located within your django project. If the following is your directory structure

- django_project
    - main
        - views.py
    - utilities_dir
        - __init__.py
        - utility_1.py
        - utility_2.py

You simply need to do this in your views.py

from utilies_dir import utility_1, utility_2

and then use the functions with utility_1.some_function() , utility_2.some_function().

Upvotes: 1

levi
levi

Reputation: 22697

Place utilities dir in your project root folder, add __init__.py file to the folder then when you want use your utilities function, just do that:

from utilities_dir.utility_1 import  *
from utilities_dir.utility_2 import  a_specific_function

Structure

- django_project
    - main
        - views.py
    - utilities_dir
        - __init__.py
        - utility_1.py
        - utility_2.py

Upvotes: 4

Related Questions