我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.utils.html.format_html_join()。
def editor_js(): js_files = [ 'javascripts/hallo.js', ] js_includes = format_html_join('\n', '<script src="{0}{1}"></script>', ((settings.STATIC_URL, filename) for filename in js_files) ) return js_includes + format_html(""" <script> registerHalloPlugin('blockQuoteButton'); registerHalloPlugin('editHtmlButton'); </script> """)
def flatatt(attrs): """ Convert a dictionary of attributes to a single string. The returned string will contain a leading space followed by key="value", XML-style pairs. In the case of a boolean value, the key will appear without a value. It is assumed that the keys do not need to be XML-escaped. If the passed dictionary is empty, then return an empty string. The result is passed through 'mark_safe' (by way of 'format_html_join'). """ key_value_attrs = [] boolean_attrs = [] for attr, value in attrs.items(): if isinstance(value, bool): if value: boolean_attrs.append((attr,)) else: key_value_attrs.append((attr, value)) return ( format_html_join('', ' {}="{}"', sorted(key_value_attrs)) + format_html_join('', ' {}', sorted(boolean_attrs)) )
def render(self, name, value, attrs): encoded = value final_attrs = self.build_attrs(attrs) if not encoded or encoded.startswith(UNUSABLE_PASSWORD_PREFIX): summary = mark_safe("<strong>%s</strong>" % ugettext("No password set.")) else: try: hasher = identify_hasher(encoded) except ValueError: summary = mark_safe("<strong>%s</strong>" % ugettext( "Invalid password format or unknown hashing algorithm.")) else: summary = format_html_join('', "<strong>{}</strong>: {} ", ((ugettext(key), value) for key, value in hasher.safe_summary(encoded).items()) ) return format_html("<div{}>{}</div>", flatatt(final_attrs), summary)
def render(self, name, value, attrs): encoded = value final_attrs = self.build_attrs(attrs) if not encoded or encoded.startswith(UNUSABLE_PASSWORD_PREFIX): summary = mark_safe("<strong>%s</strong>" % ugettext("No password set.")) else: try: hasher = identify_hasher(encoded) except ValueError: summary = mark_safe("<strong>%s</strong>" % ugettext( "Invalid password format or unknown hashing algorithm." )) else: summary = format_html_join( '', '<strong>{}</strong>: {} ', ((ugettext(key), value) for key, value in hasher.safe_summary(encoded).items()) ) return format_html("<div{}>{}</div>", flatatt(final_attrs), summary)
def get_editor_js_for_registration(cls): js_files = cls.get_editor_js_files() js_includes = format_html_join( '\n', '<script src="{0}{1}"></script>', ((settings.STATIC_URL, filename) for filename in js_files) ) return js_includes + format_html( """ <script> window.chooserUrls.{0}Chooser = '{1}'; </script> """, cls.model._meta.object_name, urlresolvers.reverse('{}_chooser'.format(cls.model._meta.model_name)) )
def flatatt(attrs): """ Convert a dictionary of attributes to a single string. The returned string will contain a leading space followed by key="value", XML-style pairs. In the case of a boolean value, the key will appear without a value. It is assumed that the keys do not need to be XML-escaped. If the passed dictionary is empty, then return an empty string. The result is passed through 'mark_safe' (by way of 'format_html_join'). """ key_value_attrs = [] boolean_attrs = [] for attr, value in attrs.items(): if isinstance(value, bool): if value: boolean_attrs.append((attr,)) elif value is not None: key_value_attrs.append((attr, value)) return ( format_html_join('', ' {}="{}"', sorted(key_value_attrs)) + format_html_join('', ' {}', sorted(boolean_attrs)) )
def editor_js(): js_files = [ static('wagtailmedia/js/media-chooser.js'), ] js_includes = format_html_join( '\n', '<script src="{0}"></script>', ((filename, ) for filename in js_files) ) return js_includes + format_html( """ <script> window.chooserUrls.mediaChooser = '{0}'; </script> """, urlresolvers.reverse('wagtailmedia:chooser') )
def tabs_block_styles(): return format_html( '<link rel="stylesheet" href="' + settings.STATIC_URL + 'wagtailblocks_tabs/css/wagtailblocks_tabs.css">' ) # @hooks.register('insert_editor_js') # def tabs_block_scripts(): # js_files = [ # 'wagtailblocks_tabs/js/tabs.js', # ] # js_includes = format_html_join( # '\n', '<script src="{0}{1}"></script>', # ((settings.STATIC_URL, filename) for filename in js_files) # ) # return js_includes + format_html( # """ # <script> # $( document ).ready(tabs_block_init()); # </script> # """ # )
def editor_js(): js_files = [ static('wagtailvideos/js/video-chooser.js'), ] js_includes = format_html_join( '\n', '<script src="{0}"></script>', ((filename, ) for filename in js_files) ) return js_includes + format_html( """ <script> window.chooserUrls.videoChooser = '{0}'; </script> """, urlresolvers.reverse('wagtailvideos:chooser') )
def per_rate_tickets(self, obj): available_rates = obj.rate_set.filter(sold_out=False, sales_open=True) available = format_html_join('\n', '<li>{} ({}/{})</li>', ((r.name, r.ticket_count, r.capacity) for r in available_rates)) sold_out_rates = obj.rate_set.filter(sold_out=True) sold_out = format_html_join('\n', '<li>{} ({}/{})</li>', ((r.name, r.ticket_count, r.capacity) for r in sold_out_rates)) closed_rates = obj.rate_set.filter(sales_open=False) closed = format_html_join('\n', '<li>{} ({}/{})</li>', ((r.name, r.ticket_count, r.capacity) for r in closed_rates)) if available == '' and sold_out == '' and closed == '': return '-' available = available if available != '' else 'No available rates' sold_out = sold_out if sold_out != '' else 'No sold out rates' closed = closed if closed != '' else 'No closed rates' return format_html('<span>Available</span><ul>{}</ul><span>Sold Out' '</span><ul>{}</ul> <span>Closed</span><ul>{}</ul>', available, sold_out, closed)
def as_html(self): ''' Render to HTML tag attributes. Example: .. code-block:: python >>> from django_tables2.utils import AttributeDict >>> attrs = AttributeDict({'class': 'mytable', 'id': 'someid'}) >>> attrs.as_html() 'class="mytable" id="someid"' :rtype: `~django.utils.safestring.SafeUnicode` object ''' blacklist = ('th', 'td', '_ordering') return format_html_join( ' ', '{}="{}"', [(k, v) for k, v in six.iteritems(self) if k not in blacklist] )
def enable_source(): js_files = [ 'js/tinymce-textcolor-plugin.js', ] js_includes = format_html_join( '\n', '<script src="{0}{1}"></script>', ((settings.STATIC_URL, filename) for filename in js_files) ) return js_includes + format_html( """ <script> registerHalloPlugin('hallohtml'); registerMCEPlugin('textcolor'); </script> """ )
def render(self, name, value, attrs): encoded = value final_attrs = self.build_attrs(attrs) if not encoded or encoded.startswith(UNUSABLE_PASSWORD_PREFIX): summary = mark_safe("<strong>%s</strong>" % ugettext("No password set.")) else: try: hasher = identify_hasher(encoded) except ValueError: summary = mark_safe("<strong>%s</strong>" % ugettext( "Invalid password format or unknown hashing algorithm.")) 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)
def html_full_contact(self): parts = [] if self.contact_name and self.contact_email: parts.append(format_html( '{name} <<a href="mailto:{email}">{email}</a>>', name=self.contact_name, email=self.contact_email )) elif self.contact_name: parts.append(self.contact_name) elif self.contact_email: parts.append(format_html('<a href="mailto:{email}">{email}</a>', email=self.contact_email)) if self.contact_phone and not self.contact_hide_phone: parts.append(self.contact_phone) if parts: return format_html_join(mark_safe(" — "), '{}', ((part,) for part in parts)) else: return format_html("<em>{}</em>", _("Pas d'informations de contact"))
def editor_js(): js_files = [ static('wagtailimages/js/hallo-plugins/hallo-wagtailimage.js'), static('wagtailimages/js/image-chooser.js'), ] js_includes = format_html_join( '\n', '<script src="{0}"></script>', ((filename, ) for filename in js_files) ) return js_includes + format_html( """ <script> window.chooserUrls.imageChooser = '{0}'; registerHalloPlugin('hallowagtailimage'); </script> """, urlresolvers.reverse('wagtailimages:chooser') )
def editor_js(): js_files = [ static('wagtaildocs/js/hallo-plugins/hallo-wagtaildoclink.js'), static('wagtaildocs/js/document-chooser.js'), ] js_includes = format_html_join( '\n', '<script src="{0}"></script>', ((filename, ) for filename in js_files) ) return js_includes + format_html( """ <script> window.chooserUrls.documentChooser = '{0}'; registerHalloPlugin('hallowagtaildoclink'); </script> """, urlresolvers.reverse('wagtaildocs:chooser') )