flask 常用功能

文件结构

|-- flask_demo
	|-- static
		|-- css
			|-- 1.css
		|-- imgs
			|-- 1.png
		|-- js
	|-- views
		|-- index.html
		|-- usetemplates.html
	|-- web.py
  • web.py 文件
from flask import Flask, render_template

app = Flask(__name__, template_folder='views')


# 视图函数绑定多个路由
@app.route('/index')
@app.route('/')
def index():
    return '<h1>hello page</h1>'


# 传入参数
# string int float path uuid
# @app.route('/home/<int:user_id>')
# @app.route('/home/<path:user_name>')
# @app.route('/home/<uuid:user_name>')
@app.route('/home/<string:user_name>')
def home(user_name):
    print(user_name, type(user_name))
    return '<h1>home page</h1>'


# 传入多个参数
@app.route('/unameid/<string:user_name>/<int:user_id>')
def unameid(user_name, user_id):
    print(user_name, type(user_name))
    print(user_id, type(user_id))
    return '<h1>home page</h1>' + user_name + str(user_id)


# 使用注册方式添加路由
def about(user_id):
    return 'about page ' + str(user_id)


app.add_url_rule(rule='/about/<int:user_id>', view_func=about)


# 返回模板
@app.route('/temp')
def temp():
    return render_template('index.html')


# 返回json
# import json
from flask import jsonify


@app.route('/getjson')
def getjson():
    user = {
        'static': '1',
        'message': 'OK'
    }

    # return json.dumps(user)
    return jsonify(user)


# 重定向
from flask import redirect


@app.route('/baidu')
def baidu():
    # 访问该路由重定向到百度
    return redirect(location='https://www.baidu.com')


# 地址反解
from flask import url_for


@app.route('/geturl')
def geturl1():
    print(url_for('getjson'))  # 无参数的视图函数

    # @app.route('/home/<string:user_name>')
    print(url_for('home', user_name='ken'))  # 带参数的视图函数
    # return '获取视图函数对应的路由'
    return redirect(url_for('home', user_name='ken'))


# 使用模板引擎
@app.route('/usetemp')
def usetemp():
    # user = '老王'
    # user = '老李'
    user = 'bill'
    vals = ['ken', 'tom', 'shirly', 'ada', 'bill']
    response_info = {'status': '1', 'message': 'Are You OK?'}

    return render_template(
        'usetemplates.html',
        user=user,
        vals=vals,
        response_info=response_info
    )


if __name__ == '__main__':
    app.debug = True
    app.run()

  • 1.css 文件
body{
    background-color: gray;
}
  • 1.png 图片

  • index.html 文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
    <link rel="stylesheet" href="/static/css/1.css">
</head>
<body>

    <h1>temp page</h1>

    <img src="static/imgs/1.png" alt="png" width="10%" height="10%">
    <img src="{{ url_for('static', filename='imgs/1.png') }}" alt="png" width="10%" height="10%">

</body>
</html>
  • useteimplates.html 文件
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>使用模板引擎</title>
</head>
<body>
    <h1>使用模板引擎</h1>

<!-- 显示python对象 -->
    {{ user }} <br>
    {{ vals }} <br>
    <hr>
    {{ response_info }} <br>

    {{ response_info['status'] }} <br>
    {{ response_info['message'] }} <br>

    {{ response_info.status }} <br>
    {{ response_info.message }} <br>

    {{ response_info.get('message') }} <br>
    {{ response_info.get('status') }} <br>

    {{ response_info.items() }} <br>

    <hr>

<!--if判断-->
    {% if user == '老王' %}
        <label>是老王</label>
    {% elif user == '老李' %}
        <label>是老李</label>
    {% else %}
        <label>不是老王或老李</label>
    {% endif %}

<!--for循环-->
    <ul>
    {% for val in vals %}
        <li>{{ val }}</li>
    {% endfor %}
    </ul>

    <ul>
    {% for k, v in response_info.items() %}
        <li>{{ k }} 键值对 {{ v }}</li>
    {% endfor %}
    </ul>

<!--过滤器-->
    {{ user | upper }} <br>
    {{ vals | count }} <br>
    {{ vals | length }} <br>


</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值