Getting started with Flask-building a minimalist blog (2)

This section will write a main page

One,

Added in app.py:

@app.route('/')
def show_entries():
    entries = Entries.query.all()
    entries_dict = [dict(title=x.title, text=x.text) for x in entries]
    return render_template('show_entries.html', entries=entries_dict)

two,

layout.html

<!doctype html>
<html>
  <head>
    <title>Flaskr</title>
    <link rel='stylesheet' type='text/css' href="{{ url_for('static', filename='style.css') }}">
  </head>

  <body>
    <div class='page'>
      <h1>Flaskr</h1>

      {% block body %}{% endblock %}
    </div>
  </body>
</html>

show_entries.html

{% extends "layout.html" %}

{% block body %}

  <ul class=entries>
  {% for entry in entries %}
    <li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}</li>
  {% else %}
    <li><em>Unbelievable. No entries here so far.</em><li>
  {% endfor %}
  </ul>

{% endblock %}

Guess you like

Origin www.cnblogs.com/holaworld/p/12672726.html