#29372: linebreaksbr and linebreaks can not work in template filters
-------------------------------------+-------------------------------------
Reporter: 何翔宇 | Owner: nobody
(Sean Ho) |
Type: Bug | Status: new
Component: Template | Version: 2.0
system | Keywords: linebreaksbr
Severity: Normal | linebreaks template-filters
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
In template, linebreaksbr and linebreaks can convert newlines in a piece
of plain text to HTML line breaks ( <br>).
They all use the same way which is "replace('\n', '<br>')", the correct
way is “"replace(r'\n', '<br>')”.
{{{
@register.filter(is_safe=True, needs_autoescape=True)
@stringfilter
def linebreaksbr(value, autoescape=True):
"""
Convert all newlines in a piece of plain text to HTML line breaks
(``<br>``).
"""
autoescape = autoescape and not isinstance(value, SafeData)
value = normalize_newlines(value)
if autoescape:
value = escape(value)
return mark_safe(value.replace('\n', '<br>'))
}}}
{{{
@keep_lazy_text
def linebreaks(value, autoescape=False):
"""Convert newlines into <p> and <br>s."""
value = normalize_newlines(value)
paras = re.split('\n{2,}', str(value))
if autoescape:
paras = ['<p>%s</p>' % escape(p).replace('\n', '<br>') for p in
paras]
else:
paras = ['<p>%s</p>' % p.replace('\n', '<br>') for p in paras]
return '\n\n'.join(paras)
}}}
Attachments is error result in html.
--
Ticket URL: <https://code.djangoproject.com/ticket/29372>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/055.ac54c38c0aa7a2ee0924a1d6497a4993%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.