码迷,mamicode.com
首页 > 其他好文 > 详细

二 Flask Project 实战 二

时间:2019-04-16 10:35:15      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:dex   com   rri   view   code   tools   res   int   strong   

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):br/>@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‘))
br/>@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‘)

二 Flask Project 实战 二

标签:dex   com   rri   view   code   tools   res   int   strong   

原文地址:https://blog.51cto.com/12408798/2379038

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!