0

My linebreaks are not working when i try to use a custom filter and safe in my web site for publishing post.
html:

  <p>{{ text|striptags|linebreaks|mention|safe }}</p>

mention is my custom filter and my templatetags is:

@register.filter(name='mention',is_safe=True)
@stringfilter
def mention(value):
 res = ""

 for i in value.split():
    if i[0] == '#':
        if len(i[1:]) > 1:
            i = f'<a href="/recommendation/?q=%23{i[1:]}&submit=Search">{i}</a>'


    res = res + i + ' '
return res

at the end i am using 'safe' so that the user can see the link.it is working but when i type linebreaks i am not seing the linebreaks inside my html.What am i doing wrong ? do i have problem in ordering my filter ?

1
  • 2
    Provide a sample text that has issue. Commented Sep 21, 2021 at 11:17

1 Answer 1

0

I’m not sure why you would like to add the linebreaks filter. Could you give an example so that I can edit this answer?

Django filters are simple functions you can test directly from a shell.

You don’t need to add the safe filter after mention because you already registered the filter as safe (which is right).

Maybe you want to add the linebreaks at the end of the filter chain, but then in your custom filter, you should split and keep the separator.

Maybe something like:

@register.filter(name='mention',is_safe=True)
@stringfilter
def mention(value):
  res = ''
  parts = re.split('(\s)', value)
  for i, elem in enumerate(parts[::2]):
      if elem and elem[0] == '#' and len(elem) > 2:
          elem = f'<a href="/recommendation/?q=%23{elem[1:]}&submit=Search">{elem}</a>'
      res += elem
      sep_index = i * 2 + 1
      if sep_index < len(parts):
          res += parts[sep_index]
return res

If a capture group is specified in re.split, then the separator is returned too (which is convenient here).

and in your template:

<p>{{ text|striptags|mention|linebreaksbr }}</p>

linebreaksbr only do that, replace \n by <br>, without wrapping the result in a p tag (as you already do it anyway).

If you define text as text='this is a\n#django \n#test' and

linebreaksbr(mention(striptags(text)))

is invoked, you got

'this is a<br><a href="/recommendation/?q=%23django&submit=Search">#django</a> <br><a href="/recommendation/?q=%23test&submit=Search">#test</a>'

Sign up to request clarification or add additional context in comments.

6 Comments

i am not able to test it because i am getting an error "string index out of range" in "if elem[0] == '#' and len(elem) > 2"
the filter that i wrote when i tried something like "This is bruxelles \n#iloveyoubruxelles" then i am getting "This is bruxelles <a href="/recommendation/?q=%23iloveyoubruxelles&submit=Search">#iloveyoubruxelles</a>" i am not getting the new line as i wrote in form post in my website.i tried everything.
i use safe at the end so that the user could see the link not the html .something like twitter does with hash tag.
maybe the problem is the "striptags" ?
safe is use to tell that the template engine does not need to escape html sequences because the string is html-safe. Without safe, the django template engine will escape some sequences, like < or > to &lt; and &gt;. So if you don’t use safe, the html will be escaped and you will see the html as text. striptags will simply remove any string starting with <, a tag and ending with either /> or </TAG_NAME>.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.