Chapter 2: Templates (v0.2)

This commit is contained in:
Miguel Grinberg 2017-09-04 23:28:11 -07:00
parent 23b3fc85d4
commit 5d4559227a
No known key found for this signature in database
GPG Key ID: 36848B262DF5F06C
3 changed files with 36 additions and 1 deletions

View File

@ -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)

15
app/templates/base.html Normal file
View File

@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
{% if title %}
<title>{{ title }} - Microblog</title>
{% else %}
<title>Welcome to Microblog</title>
{% endif %}
</head>
<body>
<div>Microblog: <a href="/index">Home</a></div>
<hr>
{% block content %}{% endblock %}
</body>
</html>

8
app/templates/index.html Normal file
View File

@ -0,0 +1,8 @@
{% extends "base.html" %}
{% block content %}
<h1>Hi, {{ user.username }}!</h1>
{% for post in posts %}
<div><p>{{ post.author.username }} says: <b>{{ post.body }}</b></p></div>
{% endfor %}
{% endblock %}