Access decorators from multiple views in Django

Viewed 20

As I am learning Django, I am running into this issue I have not found a solution for.

Imagine I have 5 apps in my project:

* project
* accounts
* profiles
* products
* services

In my accounts project, I have all of the logic for authentication and have created a decorators.py file under accounts that I would like to check from any of the apps.

Under accounts, in my views.py, I simply import the decorators.py as such:

from .decorators import *

At this point, however, I am not seeing how to import the same decorators.py from the views.py file in other apps.

1 Answers

The PYTHON_PATH is set at the project root, so you can import with:

from accounts.decorators import some_decorator

Note: Please do not use wildcard importsĀ [quantifiedcode.com]. It makes the statement less predictable, it can easily result in failing code if you later decide to change what is exported in a certain module, and furthermore it can override variables.

Related