码迷,mamicode.com
首页 > Web开发 > 详细

flask web开发实战 入门进阶和原理解析

时间:2019-02-02 10:43:47      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:sage   rip   perfect   一个   ror   web   handle   错误处理函数   block   

其实就是记录了一下用法,毕竟不懂前端。 主要记录下来后端如何调用的。

app.py

import os
from flask import Flask, render_template, flash, redirect, url_for, Markup

app = Flask(__name__)
app.secret_key = os.getenv(SECRET_KEY, secret string)


user = {
    username: dairuiquan,
    bio:  A boy who love  basketball and movies,
}

movies = [
    {name: My Neighbor Totoro, year: 1988},
    {name: Three Colours trilogy, year: 1993},
    {name: Forrest Gump, year: 1994},
    {name: Perfect Blue, year: 1997},
    {name: The Matrix, year: 1999},
    {name: Memento, year: 2000},
    {name: The Bucket list, year: 2007},
    {name: Black Swan, year: 2010},
    {name: Gone Girl, year: 2014},
    {name: CoCo, year: 2017},
]


@app.route(/watchlist)
def watchlist():
    return render_template(watchlist.html, user=user, movies=movies)


@app.route(/)
def index():
    name = abc
    return render_template(index.html, name=name)


# 注册模版上下文处理函数,如果所有模版都需要传入相同变量,调用这个视图函数
@app.context_processor
def inject_info():
    foo = I am a foo
    return dict(foo=foo)


# 注册模版全局函数
@app.template_global()
def bar():
    return I am a bar


# 自定义过滤器  &#9835是html的音符图标的转义,这个过滤器接受一个参数,返回
# 参数加上音符图标
@app.template_filter()
def musical(s):
    return s + Markup(♫)


# 自定义测试器
@app.template_test()
def baz(n):
    if n == baz:
        return True
    else:
        return False


@app.route(/watchlist2)
def watchlist_with_static():
    return render_template(watchlist_with_static.html, user = user, movies = movies)


@app.route(/flash)   # 定义闪现消息,闪现消息会显示在return的页面上
def just_flash():
    flash(I am flash, who is looking for me?)
    return redirect(url_for(index))  # flash消息显示在根目录


# 两个错误处理函数
@app.errorhandler(404)
def page_not_found(e):  # 这个传入的e是错误
    return render_template(errors/404.html), 404


@app.errorhandler(500)
def internal_server_error(e):
    return render_template(errors/500.html), 500

 

 

index.html

{% extends ‘base.html‘ %}
{% from ‘macros.html‘ import qux %}

{% block content %}
{% set name=‘baz‘ %}
<h1> Template</h1>
<ul>
    <li><a href="{{ url_for(‘watchlist‘) }}" >Watchlist </a></li>
    <li>Filter: {{ foo|musical }} </li>
    <li>Global: {{ bar() }}</li>
    <li>Test: {% if name is baz %} I am baz. {% endif %}</li>
    <li>Macro: {{ qux(amount = 5) }}</li>
    <li><a href="{{ url_for(‘watchlist_with_static‘) }}">Watchlist with image and styles.</a></li>
    <li><a href="{{ url_for(‘just_flash‘) }}">Flash something</a></li>

</ul>

{% endblock %}

 

base.html

{% block head %} {% block styles %} {% endblock %} {% endblock %}
Home
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %} {% block content %}{% endblock %}
{% block footer %} © 2018 Grey Li / GitHub / HelloFlask {% endblock %}
{% block scripts %}{% endblock %}

 

flask web开发实战 入门进阶和原理解析

标签:sage   rip   perfect   一个   ror   web   handle   错误处理函数   block   

原文地址:https://www.cnblogs.com/dairuiquan/p/10347531.html

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