Correct usage of rules py in module
hctomkins opened this issue · comments
Assuming I have a django project directory like so:
AppA:
-#views
-#models
-#rules.py
etc
AppB
-#settings
-#wsgi
etc
Rules
-compat
-contrib
-templatetags
-#apps
etc
In App A I am defining predicates in the rules.py file, which has the absolute import from future at the top. I want to use these predicates in the App A views file, so I go from 'rules.contrib.views import permission_required'. This however tries to import contrib.views from AppA/rules.py, rather than from the Rules package. What am I doing wrong?
The documentation isn't super clear about this I don't think! I have 'rules.apps.AutodiscoverRulesConfig' in my installed apps.
Traceback
File "/home/h/anaconda/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
119. resolver_match = resolver.resolve(request.path_info)
File "/home/h/anaconda/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
365. for pattern in self.url_patterns:
File "/home/h/anaconda/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
401. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/h/anaconda/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
395. self._urlconf_module = import_module(self.urlconf_name)
File "/home/h/anaconda/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/home/h/PycharmProjects/AMIproj/AMIproj/urls.py" in <module>
22. url(r'^Home/', include('AppA.urls')),
File "/home/h/anaconda/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
33. urlconf_module = import_module(urlconf_module)
File "/home/h/anaconda/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/home/h/PycharmProjects/AMIproj/AppA/urls.py" in <module>
3. from AppA import views as wrapviews
File "/home/h/PycharmProjects/AMIproj/AppA/views.py" in <module>
8. from rules.contrib.views import permission_required,objectgetter
Exception Type: ImportError at /
Exception Value: No module named contrib.views
You need to place from __future__ import absolute_import
in all files you import rules from, so that would be AppA/views.py
in this case.
Well that was easy. Thanks!
You would then be able to do this in your AppA/views.py:
from __future__ import absolute_import
from rules.contrib.views import permission_required, objectgetter
from .rules import mypredicate
No problem :)