Skip to content
pkirlin edited this page Oct 12, 2016 · 19 revisions

Using the Python SQLite interface with HTML templates

In this lesson we will build a nicer view of our blog entries, rather than the "dump" output which is ugly.

First, let's create a new HTML template (our first one for the blog!). Inside the templates/ subfolder, create a file called browse.html:

<html>
  <head>
    <title>Browse entries</title>
  </head>
<body>
  <h1>Browse entries</h1>
  {% if entries %}
  <ul>
    {% for entry in entries %}
      <li><b>{{ entry.title }}</b> ({{ entry.date }})<p>{{ entry.content }}<hr>
    {% endfor %}
  </ul>
  {% else %}
  This blog is empty.
  {% endif %}
  </ul>
</body>
</html>

To be clear, our directory structure now looks like

blog/
+-- blog.py
+-- schema.sql
+-- populate.sql
+-- blog.db (this is probably here, but technically doesn't exist until we run schema.sql)
+-- templates/
    +-- browse.html

Let's also add a new route to our blog.py code:

@app.route("/browse")
def browse():
    db = get_db()
    rows = db.execute('select id, date, title, content from entries order by date')
    return render_template('browse.html', entries=rows)

Breaking this all down

The

Use the main wiki page to navigate, not the list of pages directly above, because those are out of order.

Clone this wiki locally