我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.utils.text.Truncator()。
def label_for_value(self, value): key = self.rel.get_related_field().name try: obj = self.rel.model._default_manager.using(self.db).get(**{key: value}) except (ValueError, self.rel.model.DoesNotExist): return '' label = ' <strong>{}</strong>' text = Truncator(obj).words(14, truncate='...') try: change_url = reverse( '%s:%s_%s_change' % ( self.admin_site.name, obj._meta.app_label, obj._meta.object_name.lower(), ), args=(obj.pk,) ) except NoReverseMatch: pass # Admin not registered for target model. else: text = format_html('<a href="{}">{}</a>', change_url, text) return format_html(label, text)
def label_for_value(self, value): rel_to = self.rel.to key = self.rel.get_related_field().name try: obj = self.rel.to._default_manager.using(self.db).get(**{key: value}) related_url = reverse('admin:%s_%s_change' % (rel_to._meta.app_label, rel_to._meta.model_name), args=(value, ), current_app=self.admin_site.name) edit_str = ' <a href="%s" title="View" target="_blank">View %s</a>' % (related_url, rel_to._meta.model_name) return ' <strong>%s</strong>%s' % ( escape(Truncator(obj).words(14, truncate='...')), edit_str ) except (ValueError, self.rel.to.DoesNotExist): return ''
def label_and_url_for_value(self, value): key = self.rel.get_related_field().name try: obj = self.rel.model._default_manager.using(self.db).get(**{key: value}) except (ValueError, self.rel.model.DoesNotExist): return '', '' try: url = reverse( '%s:%s_%s_change' % ( self.admin_site.name, obj._meta.app_label, obj._meta.object_name.lower(), ), args=(obj.pk,) ) except NoReverseMatch: url = '' # Admin not registered for target model. return Truncator(obj).words(14, truncate='...'), url
def set_chars(instance, field, max_length=255): """ CharField """ possibles = [] possibles.extend(get_field_choices(field)) if possibles: setattr(instance, field.name, random.choice(possibles)) return max_length = min(max_length, field.max_length or 16 * 1024) for validator in field.validators: if isinstance(validator, MaxLengthValidator): max_length = min(max_length, validator.limit_value) value = generate_lorem_ipsum(1, min_len=1, max_len=max(5, max_length // 8), html=False) value = Truncator(value).chars(max_length, html=False) setattr(instance, field.name, value)
def label_for_value(self, value, name=None): m_value = value.split(',') m_value = [e for e in m_value if e] key = self.t_name objs = self.r_model._default_manager.using(self.db).filter(**{key+'__in': m_value}) li_format = '''<a class="btn btn-sm" onclick="removeSingleObject(this,'%s', '%s');">%s</a>''' tar_list = '' for obj in objs: if self.s_name: show_val = getattr(obj, self.s_name) if callable(show_val):show_val = show_val() show_val = escape(Truncator(show_val).words(14, truncate='...')) else: show_val = escape(Truncator(obj).words(14, truncate='...')) val = getattr(obj, key) tar_list += li_format%( 'id_'+name, val, show_val) return self.label_format %(name, tar_list )
def __str__(self): return '{0} -> {1}'.format( self.event, text.Truncator(self.url).chars(43), )
def truncate_words(s, num, end_text='...'): truncate = end_text and ' %s' % end_text or '' return Truncator(s).words(num, truncate=truncate)
def label_for_value(self, value): key = self.rel.get_related_field().name try: obj = self.rel.model._default_manager.using(self.db).get(**{key: value}) return ' <strong>%s</strong>' % escape(Truncator(obj).words(14, truncate='...')) except (ValueError, self.rel.model.DoesNotExist): return ''
def label_for_value(self, other_model, rel_name, value): try: obj = other_model._default_manager.get(**{rel_name: value}) return '%s' % escape(Truncator(obj).words(14, truncate='...')) except (ValueError, other_model.DoesNotExist): return ""
def label_for_value(self, value): key = self.rel.get_related_field().name try: obj = self.rel.to._default_manager.using( self.db).get(**{key: value}) return '%s' % escape(Truncator(obj).words(14, truncate='...')) except (ValueError, self.rel.to.DoesNotExist): return ""
def __str__(self): return Truncator(self.content).words(7)
def __str__(self): # Return the first few words of the content (with tags stripped) return Truncator(strip_tags(self.text)).words(10, truncate=' ...')
def details_court(self, detail): """ Retourne les 40 premiers caractères du contenu de l'article, suivi de points de suspension si le texte est plus long. """ return Truncator(detail.details).chars(40, truncate='...')
def __str__(self): return Truncator(strip_tags(self.body)).words(3, truncate="...")
def set_text(instance, field, paragraphs=1, html=False): """ TextField """ max_len = field.max_length or 16 * 1024 for validator in field.validators: if isinstance(validator, MaxLengthValidator): max_len = min(max_len, validator.limit_value) value = generate_lorem_ipsum(paragraphs, html=html) value = Truncator(value).chars(max_len, html=html) setattr(instance, field.name, value)
def set_slug(instance, field): """ SlugField """ possibles = [] possibles.extend(get_field_choices(field)) if possibles: setattr(instance, field.name, random.choice(possibles)) return value = slugify(generate_lorem_ipsum(1, min_len=1, max_len=6)) value = Truncator(value).chars(field.max_length, html=False) setattr(instance, field.name, value)
def label_for_value(self, value): key = self.rel.get_related_field().name try: obj = self.rel.to._default_manager.using(self.db).get(**{key: value}) return ' <strong>%s</strong>' % escape(Truncator(obj).words(14, truncate='...')) except (ValueError, self.rel.to.DoesNotExist): return ''
def log_name(self): # Required by task_log return Truncator(self.url).chars(32)
def _render_label(self, name, value): return self.label_format %(name, escape(Truncator(value).words(14, truncate='...')) )
def label_for_value(self, value, name=None): m_value = value.split(',') m_value = [e for e in m_value if e] key = self.rel.get_related_field().name objs = objs = self.rel.to._default_manager.using(self.db).filter(**{key+'__in': m_value}) li_format = '''<a class="btn btn-sm" onclick="removeSingleObject(this,'%s', '%s');">%s</a>''' tar_list = '' for obj in objs: show_val = escape(Truncator(obj).words(14, truncate='...')) val = getattr(obj, key) tar_list += li_format%( 'id_'+name, val, show_val) return self.label_format %(name, tar_list )
def video_title(self, obj): return Truncator(obj.video.title).chars(50)
def __str__(self): return Truncator(strip_tags(self.text)).words(20)
def short_description(self, obj): return Truncator(obj.description).chars(65)