0

I want to input the content of a text area and want to output it on another page but seems like there is nothing like multiline-text area in Flask. When I do the following

content = request.form['content']

it returns a string with line breaks as '\n' but when I try to output that content with replacing \n with
or , it doesn't seem to work.

So I thought I can store the multiline content in the form of a list.

So is there db column for the list, something like

content = db.Column(db.list(String))

or is there any other alternative.

0

1 Answer 1

1

Just to clarify, to the computer these 2 text examples are exactly equivalent:

myString = """Hello
World
"""
myString = "Hello\nWorld"

We can confirm this by checking the repr value for both versions

repr(myString)
# 'Hello\nWorld'

Whether or not the formatting is performed in a "friendly way" where the newlines are rendered as such, is entirely dependent on how you choose to display them. In HTML, newlines are denoted with a <br> tag, so one option would be to store the actual HTML-formatted string in your database after inserting them. However, this may pose a security hazard by either allowing malicious links to be made clickable, or by allowing Javascript snippets to be executed when rendering the page.

The simplest solution would be to use the HTML <pre> tag, which tells it that you have already handled the formatting ahead-of-time. Using the same myString value as before, we can display it nicely with

<pre>
{{ myString }}
<pre>

using the Jinja2 syntax, as long as we pass this string to the render_template function, for example

@app.route("/")
def index():
    myString = "Hello\nWorld"
    return render_template("index.html", myString=myString)
Sign up to request clarification or add additional context in comments.

Comments

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.