Links

We now have two different routes:

  1. http://localhost:5000/poem Allows us to create a new poem, save it to the database, and display it.
  2. http://localhost:5000/poems Lists all of the poems we have created.

But we shouldn’t expect a user to know those two paths to get to the content. Instead we can link those two pages together. We’re going to update our templates so that each of our views can link to each other.

First we can link from a single poem to all of our poems. Make your templates/poem.html file look like this:

<h1>Poem</h1>
<p>{{ poem }}</p>

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

<br><br>

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

Now visit http://localhost:5000/poem and you should now see a link that leads you to the list of all poems.

You can see the url_for method used here which is a helper for linking between different routes within your application.

Now from your /poems page link back to create a new poem by including the link at the top of 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 }}</p>
  <small>created at {{ poem.created_at }}</small>
{% endfor %}

Now click back and forth between your list of poems and creation of a new poem.