0

I have a problem in a Django app I'm writing. I have a view *project_detail* which should be limited to users who are project leaders of that project. I've solved that with a decorator which is defined in a separate file helpers.py. Another view error is called if an error occured. Here are the two files views.py and helpers.py

#views.py#
from projectmanagement.helpers import is_projectleader    


@is_projectleader
def project_detail(request,id):
...

def error(request, errormessage)
....
    return render_to_response(...)

--------------------------------------
#helpers.py#
def is_projectleader():
....
if not request.user in project.projectleaders:
    return error(request,errormessage="You are not a projectleader")
....

Now i'm getting an error that error is not defined, but if I add

from projectmanagement.views import error

I get an error, that I can't import error. Have you any suggestions?

Duncan Parkes
  • 1,902
  • 1
  • 15
  • 24
Moe
  • 1,021
  • 1
  • 9
  • 16
  • it was a circular import. thanks for the tips. i've just moved my error function into the file helpers.py – Moe Jul 19 '12 at 12:20

3 Answers3

1

Import the module itself, and refer to the attribute of that module.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

First - please write entire stacktrace and error message. Second - what happens if you will have everything inside one file (for example views.py). It seems like circular dependency - from views.py you're importing helpers.py; from helpers.py you're importing views.py.

Marek
  • 439
  • 2
  • 5
  • 12
1

You cannot import one module from the other and vise versa at the same time. Move the defenition of the error function to the helpers.py or create a new module for views like this one.

Pavel Reznikov
  • 2,968
  • 1
  • 18
  • 17
  • Yes you can. Python itself has no issues with circular imports. – Ignacio Vazquez-Abrams Jul 18 '12 at 08:57
  • @IgnacioVazquez-Abrams unluckily, that's not exactly true. `import smth` always work, but `from module import smth` can be troublesome. Please see `It is sometimes necessary to move imports to a function or class to avoid problems with circular imports` from [python FAQ](http://docs.python.org/faq/programming.html#what-are-the-best-practices-for-using-import-in-a-module), or http://stackoverflow.com/questions/894864/circular-dependency-in-python and http://stackoverflow.com/questions/1556387/circular-import-dependency-in-python – Stefano Jul 18 '12 at 13:22