Don't crash if there is a key error in a template. Bug 556814

This commit is contained in:
Wil Clouser 2010-04-05 11:59:48 -07:00
parent 69349e2662
commit c73d2c5518
7 changed files with 41 additions and 3 deletions

View File

@ -0,0 +1,4 @@
#, python-format
msgid "Broken %(string)s"
msgstr "Broken %(fuuu)s"

View File

@ -1 +1,7 @@
import os
# Make filepaths relative to settings.
ROOT = os.path.dirname(os.path.abspath(__file__))
path = lambda *a: os.path.join(ROOT, *a)
JINJA_CONFIG = {}

3
fabfile.py vendored
View File

@ -26,11 +26,12 @@ def doc(kind='html'):
with cd('docs'):
local('make clean %s' % kind)
def shell():
local('django-admin.py shell')
def test():
local('nosetests')
def updoc():
doc('dirhtml')
rsync_project('p/%s' % NAME, 'docs/_build/dirhtml/', delete=True)

View File

@ -4,8 +4,10 @@ import functools
from django import http
from django.conf import settings
from django.template.context import get_standard_processors
from django.utils.translation import trans_real
import jinja2
import tower
VERSION = (0, 3)
__version__ = '.'.join(map(str, VERSION))
@ -74,7 +76,14 @@ def render_to_string(request, template, context=None):
# If it's not a Template, it must be a path to be loaded.
if not isinstance(template, jinja2.environment.Template):
template = env.get_template(template)
return template.render(**context)
try:
ret = template.render(**context)
except KeyError:
_lang = trans_real.get_language()
tower.deactivate_all()
ret = template.render(**context)
tower.activate(_lang)
return ret
def load_helpers():

View File

@ -1,6 +1,11 @@
from mock import patch, sentinel
from mock import Mock, patch, sentinel
from nose.tools import eq_
from test_helpers import render
import jingo.views
from jingo import get_env
import jinja2
import tower
@patch('jingo.render')
@ -8,3 +13,15 @@ def test_direct_to_template(mock_render):
request = sentinel.request
jingo.views.direct_to_template(request, 'base.html', x=1)
mock_render.assert_called_with(request, 'base.html', {'x': 1})
def test_template_substitution_crash():
tower.activate('xx')
env = get_env()
# The localized string has the wrong variable name in it
s = '{% trans string="heart" %}Broken {{ string }}{% endtrans %}'
template = env.from_string(s)
rendered = jingo.render_to_string(Mock(), template, {})
eq_(rendered, 'Broken heart')

View File

@ -4,4 +4,5 @@ jinja2
nose
mock
-e svn+http://code.djangoproject.com/svn/django/trunk@12335#egg=Django
-e git://github.com/clouserw/tower.git#egg=tower
fabric