From 5d4559227a7f215251ed1dcd15f884443109e153 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Mon, 4 Sep 2017 23:28:11 -0700 Subject: [PATCH] Chapter 2: Templates (v0.2) --- app/routes.py | 14 +++++++++++++- app/templates/base.html | 15 +++++++++++++++ app/templates/index.html | 8 ++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 app/templates/base.html create mode 100644 app/templates/index.html diff --git a/app/routes.py b/app/routes.py index 69701e3..d486cc3 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,7 +1,19 @@ +from flask import render_template from app import app @app.route('/') @app.route('/index') def index(): - return "Hello, World!" + user = {'username': 'Miguel'} + posts = [ + { + 'author': {'username': 'John'}, + 'body': 'Beautiful day in Portland!' + }, + { + 'author': {'username': 'Susan'}, + 'body': 'The Avengers movie was so cool!' + } + ] + return render_template('index.html', title='Home', user=user, posts=posts) diff --git a/app/templates/base.html b/app/templates/base.html new file mode 100644 index 0000000..56e87be --- /dev/null +++ b/app/templates/base.html @@ -0,0 +1,15 @@ + + + + {% if title %} + {{ title }} - Microblog + {% else %} + Welcome to Microblog + {% endif %} + + +
Microblog: Home
+
+ {% block content %}{% endblock %} + + diff --git a/app/templates/index.html b/app/templates/index.html new file mode 100644 index 0000000..b580e8c --- /dev/null +++ b/app/templates/index.html @@ -0,0 +1,8 @@ +{% extends "base.html" %} + +{% block content %} +

Hi, {{ user.username }}!

+ {% for post in posts %} +

{{ post.author.username }} says: {{ post.body }}

+ {% endfor %} +{% endblock %}