niwinz / django-jinja

Simple and nonobstructive jinja2 integration with Django.
http://niwinz.github.io/django-jinja/latest/
BSD 3-Clause "New" or "Revised" License
363 stars 102 forks source link

Unicode strings in TEMPLATES settings #195

Closed MikeVL closed 2 years ago

MikeVL commented 7 years ago

My config file with unicode strings raise error TypeError: Item in ``from list'' not a string

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

TEMPLATES = [
    {
        'BACKEND': 'django_jinja.backend.Jinja2',
        'NAME': 'jinja2',
        'APP_DIRS': True,
        'DIRS': [
            PROJECT_ROOT('templates')
        ],
        'OPTIONS': {
            'match_extension': '.jinja',
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                ...
            ],
            'extensions': [
                'jinja2.ext.do',
                'jinja2.ext.loopcontrols',
                'jinja2.ext.with_',
                'jinja2.ext.i18n',
                'jinja2.ext.autoescape',
                'django_jinja.builtins.extensions.CsrfExtension',
                'django_jinja.builtins.extensions.CacheExtension',
                'django_jinja.builtins.extensions.TimezoneExtension',
                'django_jinja.builtins.extensions.UrlsExtension',
                'django_jinja.builtins.extensions.StaticFilesExtension',
                'django_jinja.builtins.extensions.DjangoFiltersExtension',
                ...
            ]
        }
    },
]

But this config working fine:

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

...

TEMPLATES = [
    {
        'BACKEND': 'django_jinja.backend.Jinja2',
        'NAME': 'jinja2',
        'APP_DIRS': True,
        'DIRS': [
            PROJECT_ROOT('templates')
        ],
        'OPTIONS': {
            'match_extension': '.jinja',
            'context_processors': [
                b'django.contrib.auth.context_processors.auth',
                b'django.contrib.messages.context_processors.messages',
                ...
            ],
            'extensions': [
                b'jinja2.ext.do',
                b'jinja2.ext.loopcontrols',
                b'jinja2.ext.with_',
                b'jinja2.ext.i18n',
                b'jinja2.ext.autoescape',
                b'django_jinja.builtins.extensions.CsrfExtension',
                b'django_jinja.builtins.extensions.CacheExtension',
                b'django_jinja.builtins.extensions.TimezoneExtension',
                b'django_jinja.builtins.extensions.UrlsExtension',
                b'django_jinja.builtins.extensions.StaticFilesExtension',
                b'django_jinja.builtins.extensions.DjangoFiltersExtension',
                ...
            ]
        }
    },
]

Prioblem with unicode strings.

niwinz commented 7 years ago

Please, paste the complete stacktrace!

MikeVL commented 7 years ago
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 177, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 230, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 289, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/usr/local/lib/python2.7/dist-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 174, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 172, in get_response
    response = response.render()
  File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 160, in render
    self.content = self.rendered_content
  File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 135, in rendered_content
    template = self._resolve_template(self.template_name)
  File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 90, in _resolve_template
    new_template = self.resolve_template(template)
  File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 82, in resolve_template
    return get_template(template, using=self.using)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 26, in get_template
    engines = _engine_list(using)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 143, in _engine_list
    return engines.all() if using is None else [engines[using]]
  File "/usr/local/lib/python2.7/dist-packages/django/template/utils.py", line 110, in all
    return [self[alias] for alias in self]
  File "/usr/local/lib/python2.7/dist-packages/django/template/utils.py", line 101, in __getitem__
    engine = engine_cls(params)
  File "/usr/local/lib/python2.7/dist-packages/django_jinja/backend.py", line 188, in __init__
    self.env = environment_cls(**options)
  File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 333, in __init__
    self.extensions = load_extensions(self, extensions)
  File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 85, in load_extensions
    extension = import_string(extension)
  File "/usr/local/lib/python2.7/dist-packages/jinja2/utils.py", line 143, in import_string
    return getattr(__import__(module, None, None, [obj]), obj)
TypeError: Item in ``from list'' not a string
niwinz commented 7 years ago

Seems not related to django-jinja, the error is happens on jinja2, the extensions list is passed directly to jinja environment constructor. :(

Maybe a solution can be convert to the bytestring the list of extensions before pass it to the jinja2 on python2...

MikeVL commented 7 years ago

May be add notes about this in docs ?

wizpig64 commented 2 years ago

closing this as we no longer support python2.