5 templates

{{ and }} is an expression {% and %} denotes a control flow statement like if and for

blocks defined here that will be overridden in the other templates {% extends 'base.html' %} tells Jinja that this template should replace the blocks from the base template.

6 Static Files

template中引入 {{ url_for('static', filename='style.css') }}

7 blog blueprint

bp = Blueprint('blog', name) #factory function from . import blog app.register_blueprint(blog.bp) #app.add_url_rule() associates the endpoint name 'index' with the / url #so that url_for('index') or url_for('blog.index') will both work, generating the same / URL either way. app.add_url_rule('/', endpoint='index')

Create

def login_required(view): @functools.wraps(view) def wrapped_view(**kwargs): if g.user is None: return redirect(url_for('auth.login'))

    return view(**kwargs)

return wrapped_view

#The login_required decorator you wrote earlier is used on the blog views. @bp.route('/create', methods=('GET', 'POST')) @login_required def create(): if request.method == 'POST': title = request.form['title'] body = request.form['body'] error = None

    if not title:
        error = 'Title is required.'

    if error is not None:
        flash(error)
    else:
        db = get_db()
        db.execute(
            'INSERT INTO post (title, body, author_id)'
            ' VALUES (?, ?, ?)',
            (title, body, g.user['id'])
        )
        db.commit()
        return redirect(url_for('blog.index'))

return render_template('blog/create.html')