def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row): "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()." top_errors = self.non_field_errors() # Errors that should be displayed above all fields. output, hidden_fields = [], [] for name, field in self.fields.items(): bf = BoundField(self, field, name) bf_errors = bf.errors # Cache in local variable. if bf.is_hidden: if bf_errors: top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors]) hidden_fields.append(unicode(bf)) else: if errors_on_separate_row and bf_errors: output.append(error_row % bf_errors) label = bf.label and bf.label_tag(escape(bf.label + ':')) or '' if field.help_text: help_text = help_text_html % field.help_text else: #raise Exception(field.widget.attrs.get('xxx','')) help_text = u'' #s = unicode(bf) #raise Exception( repr(s) ) output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text}) if top_errors: output.insert(0, error_row % top_errors) if hidden_fields: # Insert any hidden fields in the last row. str_hidden = u''.join(hidden_fields) if output: last_row = output[-1] # Chop off the trailing row_ender (e.g. '</td></tr>') and insert the hidden fields. output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender else: # If there aren't any rows in the output, just append the hidden fields. output.append(str_hidden) return u'\n'.join(output)
def _render_fieldset(self, fieldset, fields, normal_row, help_text_html, errors_on_separate_row): output, hidden_fields = [], [] if fieldset: output.append( normal_row % { 'errors': '', 'label': ' ', 'field': self.fieldset_template % { 'title': fieldset, 'section': slugify(fieldset), }, 'help_text': '', }) #for name, field in [i for i in self.fields.items() if i[0] in fields]: for name in fields: field = filter(lambda f: f[0] == name, self.fields.items())[0][1] bf = BoundField(self, field, name) bf_errors = self.error_class([ escape(error) for error in bf.errors ]) # Escape and cache in local variable. if bf.is_hidden: if bf_errors: top_errors.extend([ u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors ]) hidden_fields.append(unicode(bf)) else: if errors_on_separate_row and bf_errors: output.append(error_row % force_unicode(bf_errors)) if bf.label: label = escape(force_unicode(bf.label)) # Only add the suffix if the label does not end in # punctuation. if self.label_suffix: if label[-1] not in ':?.!': label += self.label_suffix label = bf.label_tag(label) or '' else: label = '' if field.help_text: help_text = help_text_html % force_unicode(field.help_text) else: help_text = u'' output.append( normal_row % { 'errors': force_unicode(bf_errors), 'label': force_unicode(label), 'field': unicode(bf), 'help_text': help_text, 'field_name': unicode(name), }) return output, hidden_fields
def _render_fieldset(self, fieldset, fields, normal_row, help_text_html, errors_on_separate_row): output, hidden_fields = [], [] if fieldset: output.append(normal_row%{ 'errors': '', 'label': ' ', 'field': self.fieldset_template%{ 'title': fieldset, 'section': slugify(fieldset), }, 'help_text': '', }) #for name, field in [i for i in self.fields.items() if i[0] in fields]: for name in fields: field = filter(lambda f: f[0] == name, self.fields.items())[0][1] bf = BoundField(self, field, name) bf_errors = self.error_class([escape(error) for error in bf.errors]) # Escape and cache in local variable. if bf.is_hidden: if bf_errors: top_errors.extend([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors]) hidden_fields.append(unicode(bf)) else: if errors_on_separate_row and bf_errors: output.append(error_row % force_unicode(bf_errors)) if bf.label: label = escape(force_unicode(bf.label)) # Only add the suffix if the label does not end in # punctuation. if self.label_suffix: if label[-1] not in ':?.!': label += self.label_suffix label = bf.label_tag(label) or '' else: label = '' if field.help_text: help_text = help_text_html % force_unicode(field.help_text) else: help_text = u'' output.append(normal_row % { 'errors': force_unicode(bf_errors), 'label': force_unicode(label), 'field': unicode(bf), 'help_text': help_text, 'field_name': unicode(name), }) return output, hidden_fields
def render_field(self, field): ''' Render a named field to HTML. ''' try: field_instance = self.fields[field] except KeyError: raise NoSuchFormField("Could not resolve form field '%s'." % field) bf = BoundField(self, field_instance, field) output = '' if bf.errors: # If the field contains errors, render the errors to a <ul> # using the error_list helper function. bf_errors = error_list([escape(error) for error in bf.errors]) else: bf_errors = '' if bf.is_hidden: # If the field is hidden, add it at the top of the form self.prefix.append(unicode(bf)) # If the hidden field has errors, append them to the top_errors # list which will be printed out at the top of form if bf_errors: self.top_errors.extend(bf.errors) else: # Find field + widget type css classes css_class = type(field_instance).__name__ + " " + \ type(field_instance.widget).__name__ # Add an extra class, Required, if applicable if field_instance.required: css_class += " Required" if bf.label: # The field has a label, construct <label> tag label = escape(bf.label) label = bf.label_tag(label) or '' else: label = '' if field_instance.help_text: # The field has a help_text, construct <span> tag help_text = escape(field_instance.help_text) help_text = '<span class="help_text">%s</span>' % help_text else: help_text = u'' # Finally render the field output = '<div class="field %(class)s">%(label)s%(help_text)s%(errors)s<div class="input">%(field)s</div></div>\n' % \ {'class': css_class, 'label': label, 'help_text': help_text, 'errors': bf_errors, 'field': unicode(bf)} return output