Opened 8 years ago
Closed 8 years ago
#29372 closed Bug (needsinfo)
linebreaksbr and linebreaks can not work in template filters
| Reported by: | 何翔宇(Sean Ho) | Owned by: | 何翔宇(Sean Ho) |
|---|---|---|---|
| Component: | Template system | Version: | 2.0 |
| Severity: | Normal | Keywords: | linebreaksbr linebreaks template-filters |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description
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.
Attachments (1)
Change History (4)
by , 8 years ago
| Attachment: | inhtml.png added |
|---|
comment:1 by , 8 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:2 by , 8 years ago
| Easy pickings: | set |
|---|
comment:3 by , 8 years ago
| Resolution: | → needsinfo |
|---|---|
| Status: | assigned → closed |
Note:
See TracTickets
for help on using tickets.
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.