Pretty Format

So one of the things that may have bugged you all along is that our poems don’t look much like poems. Now we’ll correct that by adding line breaks.

Update your templates as below.

Update poems.html template

templates/poems.html:

<a href="{{ url_for('poem') }}">Create New Poem</a>

<h1>My Poems</h1>

{% for poem in poems %}
  <h2>{{ poem.id }}</h2>
  <p>{{ poem.body.replace('\n', '<br>') | safe }}</p>
  <small>created at {{ poem.created_at }}</small>
{% endfor %}

Update poem.html template

templates/poem.html:

<h1>Poem</h1>
<p>{{ poem.replace('\n', '<br>') | safe }}</p>

<small>Refresh the page to get a new poem</small>

<br><br>

<a href="{{ url_for('poems') }}">See all poems</a>

Take a Look

What’s Happening?

In this case we’ll do a simple string replacement of a text line break with an HTML line break. Then we let the rendering engine know that the content is HTML safe.

safe: frameworks have features which help to try to ensure you do not display data on a page which can open you up to a security vulnerability. In this case though we know that the content is HTML safe so we can pass the replaced text through the safe filter.