3

I've been trying to modify a string before passing it to my HTML page in Flask (replacing occurrences of '\n' with '<br>'), but the typical methods I use aren't working for some reason.

finalstring = textstring.replace('\n', '<br>')
return render_template('my-form-result.html', emailresponse = finalstring)

This should work, but for some reason, nothing is replaced. How can I get this to work? Thanks!

taha
  • 722
  • 7
  • 15
Mashu
  • 79
  • 2
  • 5
  • 1
    I've never worked with Flask, but it looks like Flasks templating engine escapes HTML Structures (which is a good thing). For your use-case, you have to figure out how to give flask explicit raw-html – Felix Jul 21 '20 at 22:29
  • 1
    Does this answer your question? [Passing HTML to template using Flask/Jinja2](https://stackoverflow.com/questions/3206344/passing-html-to-template-using-flask-jinja2) – Felix Jul 21 '20 at 22:30
  • 1
    Can you show us how "textstring" looks like? – Patch Jul 21 '20 at 22:43
  • @codeflush.dev It does escape HTML, unless told to do otherwise, which is not a great idea. – Captain Jack Sparrow Jul 21 '20 at 23:09
  • What do you mean, _"nothing is replaced"_? That cannot be. Try `print(repr(finalstring))` and see if it has any `\n` in it. Explain exactly what happens incorrectly. – zvone Jul 21 '20 at 23:43
  • That's why I'm so confused. It should be working. Just tried print(repr(finalstring)) and it just printed an empty string. – Mashu Jul 21 '20 at 23:52

3 Answers3

2

A better way to replace \n in HTML is using CSS styles.

Your replace() is alright. Debug your code and make sure there is \n before replace().

To be able to view linebreaks in HTML you should use safe filter in the template. But beware that you become open to XSS attacks. To get round this problem you should escape the string before replacing the \n character. This is the code:

from flask import escape

...
...
    safe_html = str(escape(text)).replace('\n', '<br/>')
    return render_template('[HTML file].html', safe_html=safe_html)

---------
#in the template:
   <span> {{ safe_html | safe }} </span>

If you don't use the str() call before replace, then the <br/> will be scaped too. Because the return value from escape() is not string.

Mahmood Dehghan
  • 7,761
  • 5
  • 54
  • 71
1

Disclaimer: I never worked with Flask, I just looked it up and hope it does what you want to do.

So somewhere in your template my-form-result.html you should find a line containing:

{{ emailresponse }}

You can replace this with:

{% for line in emailresponse.split('\n') %}
{{ line }}
<br />
{% endfor %}

To add an br after every newline

Felix
  • 2,256
  • 2
  • 15
  • 35
0

Your replace() code is correct. Make sure you escape the HTML in the template:

{{ emailresponse|safe }}

To diagnose, try this:

finalstring = textstring.replace('\n', '<br>')
print(finalstring)
return render_template('my-form-result.html', emailresponse = finalstring)

Also, show us the source code from the web page, to see what is actually rendering in the template

GAEfan
  • 11,244
  • 2
  • 17
  • 33
  • Thanks for your response! Unfortunately, I have tried each of the recommended solutions, and nothing has worked. What could be going wrong? – Mashu Jul 21 '20 at 23:42
  • Show us the rendering of this from View Source Code from your browser. – GAEfan Jul 22 '20 at 00:04