Index: django/template/defaulttags.py
===================================================================
--- django/template/defaulttags.py	(revisão 5625)
+++ django/template/defaulttags.py	(cópia de trabalho)
@@ -69,10 +69,11 @@
         return u''
 
 class ForNode(Node):
-    def __init__(self, loopvars, sequence, reversed, nodelist_loop):
+    def __init__(self, loopvars, sequence, reversed, nodelist_loop, nodelist_loop_empty):
         self.loopvars, self.sequence = loopvars, sequence
         self.reversed = reversed
         self.nodelist_loop = nodelist_loop
+        self.nodelist_loop_empty = nodelist_loop_empty
 
     def __repr__(self):
         if self.reversed:
@@ -109,36 +110,33 @@
         if not hasattr(values, '__len__'):
             values = list(values)
         len_values = len(values)
-        if self.reversed:
-            values = reversed(values)
-        unpack = len(self.loopvars) > 1
-        for i, item in enumerate(values):
-            context['forloop'] = {
-                # shortcuts for current loop iteration number
-                'counter0': i,
-                'counter': i+1,
-                # reverse counter iteration numbers
-                'revcounter': len_values - i,
-                'revcounter0': len_values - i - 1,
-                # boolean values designating first and last times through loop
-                'first': (i == 0),
-                'last': (i == len_values - 1),
-                'parentloop': parentloop,
-            }
-            if unpack:
-                # If there are multiple loop variables, unpack the item into them.
-                context.update(dict(zip(self.loopvars, item)))
-            else:
-                context[self.loopvars[0]] = item
-            for node in self.nodelist_loop:
-                nodelist.append(node.render(context))
-            if unpack:
-                # The loop variables were pushed on to the context so pop them
-                # off again. This is necessary because the tag lets the length
-                # of loopvars differ to the length of each set of items and we
-                # don't want to leave any vars from the previous loop on the
-                # context.
-                context.pop()
+        if len_values:
+            if self.reversed:
+                values = reversed(values)
+            unpack = len(self.loopvars) > 1
+            for i, item in enumerate(values):
+                context['forloop'] = {
+                    # shortcuts for current loop iteration number
+                    'counter0': i,
+                    'counter': i+1,
+                    # reverse counter iteration numbers
+                    'revcounter': len_values - i,
+                    'revcounter0': len_values - i - 1,
+                    # boolean values designating first and last times through loop
+                    'first': (i == 0),
+                    'last': (i == len_values - 1),
+                    'parentloop': parentloop,
+                }
+                if unpack:
+                    # If there are multiple loop variables, unpack the item into them.
+                    context.update(dict(zip(self.loopvars, item)))
+                else:
+                    context[self.loopvars[0]] = item
+                for node in self.nodelist_loop:
+                    nodelist.append(node.render(context))
+        else:
+            for node in self.nodelist_loop_empty:
+                 nodelist.append(node.render(context))
         context.pop()
         return nodelist.render(context)
 
@@ -583,9 +581,14 @@
             raise TemplateSyntaxError, "'for' tag received an invalid argument: %s" % token.contents
 
     sequence = parser.compile_filter(bits[in_index+1])
-    nodelist_loop = parser.parse(('endfor',))
+    nodelist_loop = parser.parse(('else', 'endfor',))
+    token = parser.next_token()
+    if token.contents == 'else':
+        nodelist_loop_false = parser.parse(('endfor',)) #ALTERADO
+    else:
+        nodelist_loop_false = NodeList()
     parser.delete_first_token()
-    return ForNode(loopvars, sequence, reversed, nodelist_loop)
+    return ForNode(loopvars, sequence, reversed, nodelist_loop, nodelist_loop_false)
 do_for = register.tag("for", do_for)
 
 def do_ifequal(parser, token, negate):
