django-crispy-forms and django-sekizai and django-jinja2 #24

Closed
pannal opened this Issue Mar 5, 2012 · 11 comments

Projects

None yet

5 participants

@pannal
pannal commented Mar 5, 2012

Hey,

using the above combination of modules results in an error (looks like a circular import on find_template_loader):

(djangocms)cmstest@dev:~/djangocms/project/subproject/toxavis$ python manage.py runserver 0.0.0.0:8000 --settings=settings_panni
Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/devserver/management/commands/runserver.py", line 70, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/core/management/base.py", line 209, in execute
    translation.activate('en-us')
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/utils/translation/__init__.py", line 100, in activate
    return _trans.activate(language)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/utils/translation/trans_real.py", line 202, in activate
    _active.value = translation(language)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/utils/translation/trans_real.py", line 185, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/utils/translation/trans_real.py", line 162, in _fetch
    app = import_module(appname)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/cms/__init__.py", line 9, in <module>
    patch_settings()
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/cms/conf/__init__.py", line 32, in patch_settings
    post_patch_check()
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/cms/conf/patch.py", line 56, in post_patch_check
    if not validate_template(template[0], ['js', 'css']):
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django_sekizai-0.5-py2.6.egg/sekizai/helpers.py", line 86, in validate_template
    found = get_namespaces(template)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django_sekizai-0.5-py2.6.egg/sekizai/helpers.py", line 76, in get_namespaces
    compiled_template = get_template(template)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/template/loader.py", line 157, in get_template
    template, origin = find_template(template_name)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/template/loader.py", line 128, in find_template
    loader = find_template_loader(loader_name)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/template/loader.py", line 95, in find_template_loader
    mod = import_module(module)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django_jinja/loaders.py", line 7, in <module>
    from django_jinja.base import env
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django_jinja/base.py", line 173, in <module>
    env = Environment(**initial_params)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django_jinja/base.py", line 94, in __init__
    filemod = import_module(file_mod_path)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/crispy_forms/templatetags/crispy_forms_filters.py", line 8, in <module>
    from crispy_forms.helper import FormHelper
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/crispy_forms/helper.py", line 4, in <module>
    from utils import render_field
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/crispy_forms/utils.py", line 14, in <module>
    default_field_template = get_template("%s/field.html" % TEMPLATE_PACK)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/template/loader.py", line 157, in get_template
    template, origin = find_template(template_name)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/template/loader.py", line 128, in find_template
    loader = find_template_loader(loader_name)
  File "/home/cmstest/djangocms/lib/python2.6/site-packages/django/template/loader.py", line 101, in find_template_loader
    raise ImproperlyConfigured('Error importing template source loader %s: "%s"' % (loader, e))
django.core.exceptions.ImproperlyConfigured: Error importing template source loader django_jinja.loaders.FileSystemLoader: "'module' object has no attribute 'FileSystemLoader'"

TEMPLATE_LOADERS is

(
    'django_jinja.loaders.FileSystemLoader',
    'django_jinja.loaders.AppLoader',
)
@maraujop
Collaborator

Hi @pannal,

I'm not sure what to say about this stacktrace or whose fault is this out of the 3 apps. Could you please post a link to django-jinja2 project. It looks like the project is failing to load the crispy templates. This might because the paths for them are built dynamically.

Cheers,
Miguel

@pannal
pannal commented Mar 12, 2012

Hmm I rather think there is a circular import happening. In the initial phase of django-jinja as the template loader while loading crispy_forms' template tags, another get_template() call is being made, caused by crispy_forms' "default_field_template = get_template("%s/field.html" % TEMPLATE_PACK)", which results in a circular import.

@niwinz
niwinz commented Mar 25, 2012

I think that the problem really lies in the cms module with sekizai module , which tries to load templates in runserver boot time. This causes circular imports on django-cryspy-forms.

I think it is related to this: (django/template/loader.py):

120 def find_template(name, dirs=None):
121     # Calculate template_source_loaders the first time the function is executed
122     # because putting this logic in the module-level namespace may cause 
123     # circular import errors. See Django ticket #1292.
@maraujop
Collaborator
maraujop commented Apr 3, 2012

Thanks Andrei for stepping by, I didn't know what to say about this issue.

I'm gonna ask Jonas Obrist, the author of django-sekizai to look at this to see if we can find a fix for this issue.

@ojii
ojii commented Apr 21, 2012

Alright, the issue itself is in the CMS, not sekizai (the CMS just uses code from sekizai to do some heavy lifting).

The easiest solution to this issue will be to set SEKIZAI_IGNORE_VALIDATION to True.

The long term solution will probably be to find a better solution for settings/environment validation in the CMS.

@maraujop
Collaborator

Thanks Jonas for clearing this up.

I'm closing the issue as it doesn't reside in django-crispy-forms.

Miguel

@maraujop maraujop closed this Apr 22, 2012
@ojii
ojii commented Apr 22, 2012

Arguably it is partially crispy-forms fault to load templates on module-level in template tags. That might cause other issues with other apps/projects.

@maraujop
Collaborator

I understand. The reason behind module-level loading templates is speeding up rendering for people that don't use template caching. It's something allowed by Django itself for this reason.

@niwinz
niwinz commented Apr 22, 2012

The module-level loading templates is not really a problem, but to satisfy most people, we can use a lazy object:

from django.utils.functional import SimpleLazyObject
default_field_template = SimpleLazyObject(lambda: get_template("%s/field.html" % TEMPLATE_PACK))
@artscoop

Confirmation: django-crispy does not work with django-jinja, because of the crispy_forms_filters.py.
The lazy objects seems a good idea.

@maraujop maraujop added a commit that referenced this issue Jun 9, 2012
@maraujop maraujop Fixing issues with module-level template loading
See #GH-24 and #GH-58

Thanks to @niwibe for proposing this solution. The template gets loaded
only once, while it is lazy loaded, so some loaders don't have problems
with it.
937830e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment