[py3] Fixed egg template loader.

This commit is contained in:
Aymeric Augustin 2012-08-14 12:09:45 +02:00
parent dcf72835e3
commit 2ae58b20ec
1 changed files with 9 additions and 4 deletions

View File

@ -1,13 +1,15 @@
# Wrapper for loading templates from eggs via pkg_resources.resource_string.
from __future__ import unicode_literals
try:
from pkg_resources import resource_string
except ImportError:
resource_string = None
from django.conf import settings
from django.template.base import TemplateDoesNotExist
from django.template.loader import BaseLoader
from django.conf import settings
from django.utils import six
class Loader(BaseLoader):
is_usable = resource_string is not None
@ -22,9 +24,12 @@ class Loader(BaseLoader):
pkg_name = 'templates/' + template_name
for app in settings.INSTALLED_APPS:
try:
return (resource_string(app, pkg_name).decode(settings.FILE_CHARSET), 'egg:%s:%s' % (app, pkg_name))
except:
pass
resource = resource_string(app, pkg_name)
except Exception:
continue
if not six.PY3:
resource = resource.decode(settings.FILE_CHARSET)
return (resource, 'egg:%s:%s' % (app, pkg_name))
raise TemplateDoesNotExist(template_name)
_loader = Loader()