diff options
Diffstat (limited to 'django/template')
| -rw-r--r-- | django/template/context.py | 2 | ||||
| -rw-r--r-- | django/template/defaulttags.py | 35 |
2 files changed, 21 insertions, 16 deletions
diff --git a/django/template/context.py b/django/template/context.py index 6ba53f340b..8f16a95021 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -62,6 +62,8 @@ class Context(object): def update(self, other_dict): "Like dict.update(). Pushes an entire dictionary's keys and values onto the context." + if not hasattr(other_dict, '__getitem__'): + raise TypeError('other_dict must be a mapping (dictionary-like) object.') self.dicts = [other_dict] + self.dicts return other_dict diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 01c43ee86f..489faa243e 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -157,8 +157,8 @@ class ForNode(Node): return nodelist.render(context) class IfChangedNode(Node): - def __init__(self, nodelist, *varlist): - self.nodelist = nodelist + def __init__(self, nodelist_true, nodelist_false, *varlist): + self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false self._last_seen = None self._varlist = map(Variable, varlist) self._id = str(id(self)) @@ -173,20 +173,21 @@ class IfChangedNode(Node): # like an OR evaluation of the multiple variables. compare_to = [var.resolve(context) for var in self._varlist] else: - compare_to = self.nodelist.render(context) + compare_to = self.nodelist_true.render(context) except VariableDoesNotExist: compare_to = None - if compare_to != self._last_seen: + if compare_to != self._last_seen: firstloop = (self._last_seen == None) self._last_seen = compare_to context.push() context['ifchanged'] = {'firstloop': firstloop} - content = self.nodelist.render(context) + content = self.nodelist_true.render(context) context.pop() return content - else: - return '' + elif self.nodelist_false: + return self.nodelist_false.render(context) + return '' class IfEqualNode(Node): def __init__(self, var1, var2, nodelist_true, nodelist_false, negate): @@ -363,12 +364,9 @@ class URLNode(Node): try: return reverse(self.view_name, args=args, kwargs=kwargs) except NoReverseMatch: - try: - project_name = settings.SETTINGS_MODULE.split('.')[0] - return reverse(project_name + '.' + self.view_name, - args=args, kwargs=kwargs) - except NoReverseMatch: - return '' + project_name = settings.SETTINGS_MODULE.split('.')[0] + return reverse(project_name + '.' + self.view_name, + args=args, kwargs=kwargs) class WidthRatioNode(Node): def __init__(self, val_expr, max_expr, max_width): @@ -803,9 +801,14 @@ def ifchanged(parser, token): {% endfor %} """ bits = token.contents.split() - nodelist = parser.parse(('endifchanged',)) - parser.delete_first_token() - return IfChangedNode(nodelist, *bits[1:]) + nodelist_true = parser.parse(('else', 'endifchanged')) + token = parser.next_token() + if token.contents == 'else': + nodelist_false = parser.parse(('endifchanged',)) + parser.delete_first_token() + else: + nodelist_false = NodeList() + return IfChangedNode(nodelist_true, nodelist_false, *bits[1:]) ifchanged = register.tag(ifchanged) #@register.tag |
