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.
* Attachment "inhtml.png" added.
* status: new => assigned
* owner: nobody => 何翔宇(Sean Ho)
--
Ticket URL: <https://code.djangoproject.com/ticket/29372#comment:1>
* easy: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/29372#comment:2>
* status: assigned => closed
* resolution: => needsinfo
Comment:
Like I said on the PR, presenting a failing test case is the key to
convince us Django is at fault. Reopen the ticket as soon as you have it.
--
Ticket URL: <https://code.djangoproject.com/ticket/29372#comment:3>