Django

Code

Ticket #3787: 3787.diff

File 3787.diff, 1.1 kB (added by Gary Wilson <gary.wilson@gmail.com>, 2 years ago)

diff from repository root and slightly more pythonic list comprehension using enumerate() instead of range(len())

  • django/newforms/widgets.py

    old new  
    317317        if not isinstance(value, list): 
    318318            value = self.decompress(value) 
    319319        output = [] 
     320        has_id = attrs.get('id', False) 
    320321        for i, widget in enumerate(self.widgets): 
    321322            try: 
    322323                widget_value = value[i] 
    323             except KeyError: 
     324            except IndexError: 
    324325                widget_value = None 
     326            if has_id: 
     327                attrs = dict(attrs, id='%s_%s' % (has_id, i))     
    325328            output.append(widget.render(name + '_%s' % i, widget_value, attrs)) 
    326329        return self.format_output(output) 
    327330 
    328331    def value_from_datadict(self, data, name): 
    329         return [data.get(name + '_%s' % i) for i in range(len(self.widgets))] 
     332        return [widget.value_from_datadict(data, name + '_%s' % i) for i, widget in enumerate(self.widgets)] 
    330333 
    331334    def format_output(self, rendered_widgets): 
    332335        return u''.join(rendered_widgets)