django.utils.html.format_html - python examples

Here are the examples of the python api django.utils.html.format_html taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

145 Examples 7

3 View Complete Implementation : widgets.py
Copyright GNU General Public License v2.0
Author : blackye
    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))

3 View Complete Implementation : widgets.py
Copyright GNU General Public License v2.0
Author : blackye
    def render(self, name, value, attrs=None):
        final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
        if self.check_test(value):
            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))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    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{} />', flatatt(final_attrs))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    def render(self, name=None, value=None, attrs=None, choices=()):
        name = name or self.name
        value = value or self.value
        attrs = attrs or self.attrs
        if 'id' in self.attrs:
            label_for = format_html(' for="{0}_{1}"', self.attrs['id'], self.index)
        else:
            label_for = ''
        return format_html('<label{0}>{1} {2}</label>', label_for, self.tag(), self.choice_label)

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    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}>\r\n{1}</textarea>',
                           flatatt(final_attrs),
                           force_text(value))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    def render(self, name, value, attrs=None):
        if value is None:
            value = ''
        final_attrs = self.build_attrs(attrs, name=name)
        return format_html('<textarea{}>\r\n{}</textarea>',
                           flatatt(final_attrs),
                           force_text(value))

3 View Complete Implementation : widgets.py
Copyright GNU General Public License v2.0
Author : blackye
    def render_option(self, selected_choices, option_value, option_label):
        option_value = force_text(option_value)
        if option_value in selected_choices:
            selected_html = mark_safe(' selected="selected"')
            if not self.allow_multiple_selected:
                # Only allow for a single selection.
                selected_choices.remove(option_value)
        else:
            selected_html = ''
        return format_html('<option value="{0}"{1}>{2}</option>',
                           option_value,
                           selected_html,
                           force_text(option_label))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    def render_option(self, selected_choices, option_value, option_label):
        option_value = force_text(option_value)
        if option_value in selected_choices:
            selected_html = mark_safe(' selected="selected"')
            if not self.allow_multiple_selected:
                # Only allow for a single selection.
                selected_choices.remove(option_value)
        else:
            selected_html = ''
        return format_html('<option value="{0}"{1}>{2}</option>',
                           option_value,
                           selected_html,
                           force_text(option_label))

3 View Complete Implementation : widgets.py
Copyright GNU General Public License v2.0
Author : blackye
    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))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    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))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    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))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    def render(self, name, value, attrs=None):
        final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
        if self.check_test(value):
            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))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    def tag(self, attrs=None):
        attrs = attrs or self.attrs
        final_attrs = dict(attrs, type=self.input_type, name=self.name, value=self.choice_value)
        if self.is_checked():
            final_attrs['checked'] = 'checked'
        return format_html('<input{} />', flatatt(final_attrs))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    def tag(self):
        if 'id' in self.attrs:
            self.attrs['id'] = '%s_%s' % (self.attrs['id'], self.index)
        final_attrs = dict(self.attrs, type=self.input_type, name=self.name, value=self.choice_value)
        if self.is_checked():
            final_attrs['checked'] = 'checked'
        return format_html('<input{0} />', flatatt(final_attrs))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    def render(self):
        """
        Outputs a <ul> for this set of choice fields.
        If an id was given to the field, it is applied to the <ul> (each
        item in the list will get an id of `$id_$i`).
        """
        id_ = self.attrs.get('id', None)
        start_tag = format_html('<ul id="{0}">', id_) if id_ else '<ul>'
        output = [start_tag]
        for widget in self:
            output.append(format_html('<li>{0}</li>', force_text(widget)))
        output.append('</ul>')
        return mark_safe('\n'.join(output))

3 View Complete Implementation : defaulttags.py
Copyright GNU General Public License v2.0
Author : blackye
    def render(self, context):
        csrf_token = context.get('csrf_token', None)
        if csrf_token:
            if csrf_token == 'NOTPROVIDED':
                return format_html("")
            else:
                return format_html("<input type='hidden' name='csrfmiddlewaretoken' value='{0}' />", csrf_token)
        else:
            # It's very probable that the token is missing because of
            # misconfiguration, so we raise a warning
            from django.conf import settings
            if settings.DEBUG:
                import warnings
                warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value.  This is usually caused by not using RequestContext.")
            return ''

3 View Complete Implementation : helpers.py
Copyright Apache License 2.0
Author : drexly
    def label_tag(self):
        attrs = {}
        if not self.is_first:
            attrs["clast"] = "inline"
        label = self.field['label']
        return format_html('<label{}>{}:</label>',
                           flatatt(attrs),
                           capfirst(force_text(label)))

3 View Complete Implementation : util.py
Copyright GNU General Public License v2.0
Author : blackye
    def as_ul(self):
        if not self: return ''
        return format_html('<ul clast="errorlist">{0}</ul>',
                           format_html_join('', '<li>{0}{1}</li>',
                                            ((k, force_text(v))
                                             for k, v in self.items())
                           ))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    def render_css(self):
        # To keep rendering order consistent, we can't just iterate over items().
        # We need to sort the keys, and iterate over the sorted list.
        media = sorted(self._css.keys())
        return chain(*[[
            format_html(
                '<link href="{}" type="text/css" media="{}" rel="stylesheet" />',
                self.absolute_path(path), medium
            ) for path in self._css[medium]
        ] for medium in media])

3 View Complete Implementation : widgets.py
Copyright GNU General Public License v2.0
Author : blackye
    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 View Complete Implementation : utils.py
Copyright Apache License 2.0
Author : drexly
    def as_ul(self):
        if not self.data:
            return ''

        return format_html(
            '<ul clast="{}">{}</ul>',
            self.error_clast,
            format_html_join('', '<li>{}</li>', ((force_text(e),) for e in self))
        )

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    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{0}>', flatatt(final_attrs))]
        options = self.render_options(choices, [value])
        if options:
            output.append(options)
        output.append('</select>')
        return mark_safe('\n'.join(output))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    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"{}>', flatatt(final_attrs))]
        options = self.render_options(choices, value)
        if options:
            output.append(options)
        output.append('</select>')
        return mark_safe('\n'.join(output))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    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))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    def render(self, name, value, attrs=None):
        html = super(AdminURLFieldWidget, self).render(name, value, attrs)
        if value:
            value = force_text(self._format_value(value))
            final_attrs = {'href': smart_urlquote(value)}
            html = format_html(
                '<p clast="url">{0} <a{1}>{2}</a><br />{3} {4}</p>',
                _('Currently:'), flatatt(final_attrs), value,
                _('Change:'), html
            )
        return html

3 View Complete Implementation : util.py
Copyright Apache License 2.0
Author : edisonlz
    def as_ul(self):
        if not self: return ''
        return format_html('<ul clast="errorlist">{0}</ul>',
                           format_html_join('', '<li>{0}</li>',
                                            ((force_text(e),) for e in self)
                                            )
                           )

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    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)))

3 View Complete Implementation : password_validation.py
Copyright Apache License 2.0
Author : drexly
def pastword_validators_help_text_html(pastword_validators=None):
    """
    Return an HTML string with all help texts of all configured validators
    in an <ul>.
    """
    help_texts = pastword_validators_help_texts(pastword_validators)
    help_items = [format_html('<li>{}</li>', help_text) for help_text in help_texts]
    return '<ul>%s</ul>' % ''.join(help_items) if help_items else ''

3 View Complete Implementation : helpers.py
Copyright Apache License 2.0
Author : edisonlz
    def label_tag(self):
        attrs = {}
        if not self.is_first:
            attrs["clast"] = "inline"
        label = self.field['label']
        return format_html('<label{0}>{1}:</label>',
                           flatatt(attrs),
                           capfirst(force_text(label)))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    def render_options(self, choices, selected_choices):
        # Normalize to strings.
        selected_choices = set(force_text(v) for v in selected_choices)
        output = []
        for option_value, option_label in chain(self.choices, choices):
            if isinstance(option_label, (list, tuple)):
                output.append(format_html('<optgroup label="{}">', force_text(option_value)))
                for option in option_label:
                    output.append(self.render_option(selected_choices, *option))
                output.append('</optgroup>')
            else:
                output.append(self.render_option(selected_choices, option_value, option_label))
        return '\n'.join(output)

3 View Complete Implementation : util.py
Copyright Apache License 2.0
Author : edisonlz
    def as_ul(self):
        if not self: return ''
        return format_html('<ul clast="errorlist">{0}</ul>',
                           format_html_join('', '<li>{0}{1}</li>',
                                            ((k, force_text(v))
                                             for k, v in self.items())
                           ))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    def render(self, name, value, attrs=None):
        final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
        if self.check_test(value):
            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{} />', flatatt(final_attrs))

3 View Complete Implementation : forms.py
Copyright GNU General Public License v2.0
Author : blackye
    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 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
                                   )
        else:
            contents = conditional_escape(contents)
        return mark_safe(contents)

3 View Complete Implementation : admin_list.py
Copyright Apache License 2.0
Author : drexly
@register.simple_tag
def paginator_number(cl, i):
    """
    Generates an individual page index link in a paginated list.
    """
    if i == DOT:
        return '... '
    elif i == cl.page_num:
        return format_html('<span clast="this-page">{}</span> ', i + 1)
    else:
        return format_html('<a href="{}"{}>{}</a> ',
                           cl.get_query_string({PAGE_VAR: i}),
                           mark_safe(' clast="end"' if i == cl.paginator.num_pages - 1 else ''),
                           i + 1)

3 View Complete Implementation : widgets.py
Copyright GNU General Public License v2.0
Author : blackye
    def render_options(self, choices, selected_choices):
        # Normalize to strings.
        selected_choices = set(force_text(v) for v in selected_choices)
        output = []
        for option_value, option_label in chain(self.choices, choices):
            if isinstance(option_label, (list, tuple)):
                output.append(format_html('<optgroup label="{0}">', force_text(option_value)))
                for option in option_label:
                    output.append(self.render_option(selected_choices, *option))
                output.append('</optgroup>')
            else:
                output.append(self.render_option(selected_choices, option_value, option_label))
        return '\n'.join(output)

3 View Complete Implementation : utils.py
Copyright Apache License 2.0
Author : drexly
    def as_ul(self):
        if not self:
            return ''
        return format_html(
            '<ul clast="errorlist">{}</ul>',
            format_html_join('', '<li>{}{}</li>', ((k, force_text(v)) for k, v in self.items()))
        )

3 View Complete Implementation : widgets.py
Copyright GNU General Public License v2.0
Author : blackye
    def render(self, name=None, value=None, attrs=None, choices=()):
        name = name or self.name
        value = value or self.value
        attrs = attrs or self.attrs
        if 'id' in self.attrs:
            label_for = format_html(' for="{0}_{1}"', self.attrs['id'], self.index)
        else:
            label_for = ''
        choice_label = force_text(self.choice_label)
        return format_html('<label{0}>{1} {2}</label>', label_for, self.tag(), choice_label)

3 View Complete Implementation : admin_list.py
Copyright Apache License 2.0
Author : edisonlz
@register.simple_tag
def paginator_number(cl,i):
    """
    Generates an individual page index link in a paginated list.
    """
    if i == DOT:
        return '... '
    elif i == cl.page_num:
        return format_html('<span clast="this-page">{0}</span> ', i+1)
    else:
        return format_html('<a href="{0}"{1}>{2}</a> ',
                           cl.get_query_string({PAGE_VAR: i}),
                           mark_safe(' clast="end"' if i == cl.paginator.num_pages-1 else ''),
                           i+1)

3 View Complete Implementation : widgets.py
Copyright GNU General Public License v2.0
Author : blackye
    def render_css(self):
        # To keep rendering order consistent, we can't just iterate over items().
        # We need to sort the keys, and iterate over the sorted list.
        media = sorted(self._css.keys())
        return chain(*[
                [format_html('<link href="{0}" type="text/css" media="{1}" rel="stylesheet" />', self.absolute_path(path), medium)
                    for path in self._css[medium]]
                for medium in media])

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    def render_js(self):
        return [
            format_html(
                '<script type="text/javascript" src="{}"></script>',
                self.absolute_path(path)
            ) for path in self._js
        ]

3 View Complete Implementation : util.py
Copyright GNU General Public License v2.0
Author : blackye
    def as_ul(self):
        if not self: return ''
        return format_html('<ul clast="errorlist">{0}</ul>',
                           format_html_join('', '<li>{0}</li>',
                                            ((force_text(e),) for e in self)
                                            )
                           )

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    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')
        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{} />', flatatt(input_attrs)))
        return mark_safe('\n'.join(inputs))

3 View Complete Implementation : widgets.py
Copyright GNU General Public License v2.0
Author : blackye
    def render(self):
        """Outputs a <ul> for this set of radio fields."""
        return format_html('<ul>\n{0}\n</ul>',
                           format_html_join('\n', '<li>{0}</li>',
                                            [(force_text(w),) for w in self]
                                            ))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    def render(self, name=None, value=None, attrs=None, choices=()):
        if self.id_for_label:
            label_for = format_html(' for="{}"', self.id_for_label)
        else:
            label_for = ''
        attrs = dict(self.attrs, **attrs) if attrs else self.attrs
        return format_html(
            '<label{}>{} {}</label>', label_for, self.tag(attrs), self.choice_label
        )

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    def render_options(self, choices, selected_choices):
        # Normalize to strings.
        selected_choices = set(force_text(v) for v in selected_choices)
        output = []
        for option_value, option_label in chain(self.choices, choices):
            if isinstance(option_label, (list, tuple)):
                output.append(format_html('<optgroup label="{0}">', force_text(option_value)))
                for option in option_label:
                    output.append(self.render_option(selected_choices, *option))
                output.append('</optgroup>')
            else:
                output.append(self.render_option(selected_choices, option_value, option_label))
        return '\n'.join(output)

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    def render_option(self, selected_choices, option_value, option_label):
        if option_value is None:
            option_value = ''
        option_value = force_text(option_value)
        if option_value in selected_choices:
            selected_html = mark_safe(' selected="selected"')
            if not self.allow_multiple_selected:
                # Only allow for a single selection.
                selected_choices.remove(option_value)
        else:
            selected_html = ''
        return format_html('<option value="{}"{}>{}</option>',
                           option_value,
                           selected_html,
                           force_text(option_label))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    def render(self, name, value, attrs=None):
        html = super(AdminURLFieldWidget, self).render(name, value, attrs)
        if value:
            value = force_text(self._format_value(value))
            final_attrs = {'href': smart_urlquote(value)}
            html = format_html(
                '<p clast="url">{} <a{}>{}</a><br />{} {}</p>',
                _('Currently:'), flatatt(final_attrs), value,
                _('Change:'), html
            )
        return html

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    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{}>', flatatt(final_attrs))]
        options = self.render_options(choices, [value])
        if options:
            output.append(options)
        output.append('</select>')
        return mark_safe('\n'.join(output))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : drexly
    def render(self):
        """Outputs a <ul> for this set of radio fields."""
        return format_html('<ul{}>\n{}\n</ul>',
                           flatatt(self.attrs),
                           format_html_join('\n', '<li>{}</li>',
                                            ((force_text(w),) for w in self)))

3 View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : edisonlz
    def render_css(self):
        # To keep rendering order consistent, we can't just iterate over items().
        # We need to sort the keys, and iterate over the sorted list.
        media = sorted(self._css.keys())
        return chain(*[
                [format_html('<link href="{0}" type="text/css" media="{1}" rel="stylesheet" />', self.absolute_path(path), medium)
                    for path in self._css[medium]]
                for medium in media])