我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用django.template.defaultfilters.linebreaks()。
def render(self, context): s = '' try: sponsor = self.sponsor_var.resolve(context) content_type = '%s_%s' % (self.content_type, context['request'].LANGUAGE_CODE) texts = sponsor.sponsor_benefits.filter(benefit__content_type=content_type) if texts.count() > 0: s = linebreaks(urlize(texts[0].text, autoescape=True)) if self.content_var: context[self.content_var] = s s = '' except: pass return s
def linebreaks_and_paragraphs(value): from django.template.defaultfilters import linebreaks return linebreaks(value)
def linebreaks(value): from django.template.defaultfilters import linebreaksbr return linebreaksbr(value)
def environment(**options): env = Environment(**options) env.install_gettext_translations(translation, newstyle=True) from .templatetags.borgcube import get_url, field_name, compression_name, summarize_archive, job_outcome, format_timedelta, json, describe_recurrence from django.utils.html import escapejs from django.template.defaultfilters import linebreaks, linebreaksbr, yesno env.filters.update({ 'get_url': get_url, # TODO: remove 'field_name': field_name, 'compression_name': compression_name, 'summarize_archive': summarize_archive, 'job_outcome': job_outcome, 'format_timedelta': format_timedelta, 'json': json, 'describe_recurrence': describe_recurrence, 'escapejs': escapejs, 'linebreaks': linebreaks, 'linebreaksbr': linebreaksbr, 'yesno': yesno, }) hook.borgcube_web_jinja_env(env=env) return env
def item_content(self, notification): return {"type": "html"}, linebreaks(escape(notification.message))
def migrate_old_posts(): """ Converts all old posts from a simple page format to one Wagtail accepts """ # id=4 is the specific page ID for the news index page index = Page.objects.get(id=4).specific old_posts = Communication.objects.using('old_data').all().order_by('date') user = get_user_model().objects.get(id=1) for post in old_posts: if post.title: title = post.title else: title = 'Archived item from {date}'.format(date=date(post.date, 'D jS F Y')) slug = slugify('{title} - {rand}'.format(title=title, rand=int(round(time.time() * 1000)))) if len(post.text) > 512: intro = post.text[:512] + '...' else: intro = post.text page = BlogPage( search_description='', seo_title=title, show_in_menus=False, slug=slug, title=title, date=post.date, first_published_at=post.date, intro=linebreaks(intro), ) page.body.stream_data = [ ('paragraph', RichText('<p>{body}</p>'.format(body=linebreaks(post.text)))) ] page.tags.add(COMMS_DICT[post.type]) print('Restoring article from {date}'.format(date=post.date)) index.add_child(instance=page) revision = page.save_revision( user=user, submitted_for_moderation=False ) revision.publish() page.save()
def migrate_events(): event_index = Page.objects.get(id=6).specific user = get_user_model().objects.get(id=1) old_events = OldEvent.objects.using('old_data').all() # Migrate events for old_event in old_events: old_event_type = old_event.type try: # We don't actually care about this - its a test to migrate the event across event_type = EventType.objects.get(name=old_event_type.name, target=old_event_type.target) except EventType.DoesNotExist: event_type = EventType(name=old_event_type.name, target=old_event_type.target) event_type.save() title = '{type} on {date}'.format(type=old_event_type.name, date=date(old_event.start, 'D jS F Y')) slug = slugify('{title} - {rand}'.format(title=title, rand=int(round(time.time() * 1000)))) if old_event.shortDescription: description = old_event.shortDescription else: if old_event_type.info: description = old_event_type.info else: description = old_event_type.name new_event = EventPage( title=title.strip(), slug=slug, description=description.strip(), start=old_event.start, finish=old_event.finish, cancelled=old_event.cancelled, category=event_type, location=old_event.location.name ) new_event.body.stream_data = [ ('paragraph', RichText('<p>{body}</p>'.format(body=linebreaks(old_event.longDescription)))) ] print('Restoring event {type} from {date}'.format(type=old_event.type.name, date=old_event.start)) event_index.add_child(instance=new_event) revision = new_event.save_revision( user=user, submitted_for_moderation=False ) revision.publish() new_event.save() # Deal with signups old_signups = Signup.objects.using('old_data').filter(event_id=old_event.id) for old_signup in old_signups: print('Restoring signup for {type} from {date}'.format(type=old_event.type.name, date=old_event.start)) new_signup = EventSignup(comment=truncatechars(old_signup.comment, 1024), member=get_user_model().objects.get(id=old_signup.user_id), event_id=new_event.id, signup_created=old_signup.time) new_signup.save()