示例#1
0
    def test_flatatt(self):
        ###########
        # flatatt #
        ###########

        self.assertEqual(flatatt({'id': "header"}), ' id="header"')
        self.assertEqual(flatatt({'class': "news", 'title': "Read this"}), ' class="news" title="Read this"')
        self.assertEqual(flatatt({}), '')
示例#2
0
 def tag(self):
     if 'id' in self.attrs:
         self.attrs['id'] = '%s_%s' % (self.attrs['id'], self.index)
     final_attrs = dict(self.attrs, type='radio', name=self.name, value=self.choice_value)
     if self.is_checked():
         final_attrs['checked'] = 'checked'
     return format_html('<input{0} />', flatatt(final_attrs))
示例#3
0
 def label_tag(self):
     attrs = {}
     if not self.is_first:
         attrs["class"] = "inline"
     label = self.field['label']
     return format_html('<label{0}>{1}:</label>',
                        flatatt(attrs),
                        capfirst(force_text(label)))
示例#4
0
 def render(self, name, value, attrs=None):
     if value is None:
         value = ''
     final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
     if value != '':
         # Only add the 'value' attribute if a value is non-empty.
         final_attrs['value'] = force_text(self._format_value(value))
     return format_html('<input{0} />', flatatt(final_attrs))
示例#5
0
 def render(self, name, value, attrs=None, choices=()):
     if value is None: value = []
     final_attrs = self.build_attrs(attrs, name=name)
     output = [format_html('<select multiple="multiple"{0}>', flatatt(final_attrs))]
     options = self.render_options(choices, value)
     if options:
         output.append(options)
     output.append('</select>')
     return mark_safe('\n'.join(output))
示例#6
0
 def render(self, name, value, attrs=None):
     final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
     try:
         result = self.check_test(value)
     except: # Silently catch exceptions
         result = False
     if result:
         final_attrs['checked'] = 'checked'
     if not (value is True or value is False or value is None or value == ''):
         # Only add the 'value' attribute if a value is non-empty.
         final_attrs['value'] = force_text(value)
     return format_html('<input{0} />', flatatt(final_attrs))
示例#7
0
 def render(self, name, value, attrs=None, choices=()):
     if value is None: value = []
     final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
     id_ = final_attrs.get('id', None)
     inputs = []
     for i, v in enumerate(value):
         input_attrs = dict(value=force_text(v), **final_attrs)
         if id_:
             # An ID attribute was given. Add a numeric index as a suffix
             # so that the inputs don't all have the same ID attribute.
             input_attrs['id'] = '%s_%s' % (id_, i)
         inputs.append(format_html('<input{0} />', flatatt(input_attrs)))
     return mark_safe('\n'.join(inputs))
示例#8
0
    def label_tag(self, contents=None, attrs=None):
        """
        Wraps the given contents in a <label>, if the field has an ID attribute.
        contents should be 'mark_safe'd to avoid HTML escaping. If contents
        aren't given, uses the field's HTML-escaped label.

        If attrs are given, they're used as HTML attributes on the <label> tag.
        """
        contents = contents or conditional_escape(self.label)
        widget = self.field.widget
        id_ = widget.attrs.get('id') or self.auto_id
        if id_:
            attrs = attrs and flatatt(attrs) or ''
            contents = format_html('<label for="{0}"{1}>{2}</label>',
                                   widget.id_for_label(id_), attrs, contents
                                   )
        return mark_safe(contents)
示例#9
0
    def render(self, name, value, attrs):
        encoded = value

        if not is_password_usable(encoded):
            return "None"

        final_attrs = self.build_attrs(attrs)

        try:
            hasher = identify_hasher(encoded)
        except ValueError:
            summary = mark_safe("<strong>Invalid password format or unknown hashing algorithm.</strong>")
        else:
            summary = format_html_join('',
                                       "<strong>{0}</strong>: {1} ",
                                       ((ugettext(key), value)
                                        for key, value in hasher.safe_summary(encoded).items())
                                       )

        return format_html("<div{0}>{1}</div>", flatatt(final_attrs), summary)
示例#10
0
 def render(self, name, value, attrs=None):
     if value is None: value = ''
     final_attrs = self.build_attrs(attrs, name=name)
     return format_html('<textarea{0}>{1}</textarea>',
                        flatatt(final_attrs),
                        force_text(value))
示例#11
0
 def render(self):
     """Outputs a <ul> for this set of radio fields."""
     return format_html('<ul{0}>\n{1}\n</ul>',
                        flatatt(self.attrs),
                        format_html_join('\n', '<li>{0}</li>',
                                         ((force_text(w),) for w in self)))