flask模板,路由,消息提示,异常处理
1.flask的路由与反向路由
- from flask import Flask, request, url_for
- app = Flask(__name__)
- @app.route('/')
- def hello_world():
- return 'Hello World!'
- @app.route('/user', methods=['POST']) # 指定请求方式,默认为GET
- def hell_user():
- return 'hello user'
- @app.route('/user/<id>') # http://127.0.0.1:5000/user/123
- def user(id):
- return 'user_id:' + id
- @app.route('/user_id') # http://127.0.0.1:5000/user_id?id=2
- def user_id():
- id = request.args.get('id')
- return 'user_id:' + id
- # 反向路由
- @app.route('/user_url')
- def user_url():
- return 'user_url:' + url_for('user_id')
- if __name__ == '__main__':
- app.run()
flask路由与反向路由
2.flask模板
- # views试图
- from flask import Flask, request, url_for, render_template
- app = Flask(__name__)
- @app.route('/index')
- def index():
- content = 'hello world'
- return render_template('index.html', content=content)
- if __name__ == '__main__':
- app.run()
- # html页面
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="x-ua-compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>flask模板示例</title>
- </head>
- <body>
- <h3>{{ content }}</h3>
- </body>
- </html>
flask模板
- # views试图
- from flask import Flask, request, url_for, render_template
- @app.route('/index')
- def index():
- content = 'hello world'
- return render_template('index.html', content=content)
- if __name__ == '__main__':
- app.run()
- # index.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="x-ua-compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>flask模板示例</title>
- </head>
- <body>
- <h3>{{ content }}</h3>
- </body>
- </html>
数据渲染
4.flask条件语句
- # views试图
- from flask import Flask, request, url_for, render_template
- from models import User
- app = Flask(__name__)
- @app.route('/query/<user_id>')
- def query(user_id):
- user = None
- if int(user_id) == 1:
- user = User(1, 'wang')
- return render_template('if_else.html', user=user)
- if __name__ == '__main__':
- app.run()
- # if_else.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="x-ua-compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>flask模板示例</title>
- </head>
- <body>
- {% if user %}
- <h2>hello: {{ user.user_name }}</h2>
- {% else %}
- <h3>hello: anonymous</h3>
- {% endif %}
- </body>
- </html>
- # models.py
- class User(object):
- def __init__(self, user_id, user_name):
- self.user_id = user_id
- self.user_name = user_name
flask条件语句
5.flask循环语句
- # views试图
- from flask import Flask, request, url_for, render_template
- from models import User
- app = Flask(__name__)
- @app.route('/user_list')
- def user_list():
- users = []
- for i in range(1, 21):
- user = User(i, 'wang' + str(i))
- users.append(user)
- return render_template('user_list.html', users=users)
- if __name__ == '__main__':
- app.run()
- # user_list.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="x-ua-compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>flask模板示例</title>
- </head>
- <body>
- {% for user in users %}
- <h4>{{ user.user_id }}--{{ user.user_name }}</h4>
- {% endfor %}
- </body>
- </html>
- # models.py
- class User(object):
- def __init__(self, user_id, user_name):
- self.user_id = user_id
- self.user_name = user_name
flask循环语句
6.flask模板继承
- # views试图
- from flask import Flask, request, url_for, render_template
- from models import User
- app = Flask(__name__)
- @app.route('/inherit_one')
- def inherit_one():
- return render_template('inherit_one.html')
- @app.route('/inherit_two')
- def inherit_two():
- return render_template('inherit_two.html')
- if __name__ == '__main__':
- app.run()
- # base.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="x-ua-compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>flask模板的继承</title>
- </head>
- <body>
- <h1>HEAD</h1>
- {% block content %}
- {% endblock %}
- <h1>FOOT</h1>
- </body>
- </html>
- # inherit_one.html
- {% extends 'base.html' %}
- {% block content %}
- <h3>this is Num.1 page</h3>
- {% endblock %}
- # inherit_two.html
- {% extends 'base.html' %}
- {% block content %}
- <h3>this is Num.2 page</h3>
- {% endblock %}
flask模板机继承
7.flask消息提示
首先导入flash
from flask import Flask, flash
然后,记得
app.secret_key = 'xxx' # flask会使用secret_key对消息进行加密
flash返回是的列表,前端注意取值
{{ get_flashed_messages()[0] }}
- # views试图
- from flask import Flask, flash, render_template, request,abort
- app = Flask(__name__) # type:Flask
- app.secret_key = ' # flask会使用secret_key对消息进行加密
- @app.route('/')
- def index():
- return render_template('login.html')
- @app.route('/login',methods=['POST'])
- def login():
- user = request.form.get('username')
- pwd = request.form.get('password')
- if not user:
- flash('please input username')
- return render_template('login.html')
- if not pwd:
- flash('please input password')
- return render_template('login.html')
- ':
- flash('login successful')
- return render_template('login.html')
- else:
- flash('username or password wrong')
- return render_template('login.html')
- # login.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="x-ua-compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>login</title>
- </head>
- <body>
- <form action="/login" method="post">
- <input type="text" name="username">
- <input type="password" name="password">
- <input type="submit" value="Submit">
- </form>
- <h2>{{ get_flashed_messages()[0] }}</h2>
- </body>
- </html>
消息提示
8.flask自定义异常处理:用户输入不合法
End
flask模板,路由,消息提示,异常处理的更多相关文章
- flask模板应用-消息闪现(flash())
消息闪现 flask提供了一个非常有用的flash()函数,它可以用来“闪现”需要提示给用户的消息,比如当用户登录成功后显示“欢迎回来!”.在视图函数调用flash()函数,传入消息内容,flash( ...
- 实验2、Flask模板、表单、视图和重定向示例
实验内容 1. 实验内容 表单功能与页面跳转功 能是Web应用程序的基础功能,学习并使用他们能够更好的完善应用程序的功能.Flask使用了名为Jinja2的模板引擎,该引擎根据用户的交互级别显示应用程 ...
- Flask中路由系统以及蓝图的使用
一.Flask的路由系统 1.@app.route()装饰器中的参数 methods:当前URL地址,允许访问的请求方式 @app.route("/info", methods=[ ...
- Flask 的路由系统 FBV 与 CBV
Flask的路由系统 本质: 带参数的装饰器 传递函数后 执行 add_url_rule 方法 将 函数 和 url 封装到一个 Rule对象 将Rule对象 添加到 app.url_map(Map对 ...
- Flask框架flash消息闪现学习与优化符合闪现之名
Flask的flash 第一次知道Flask有flash这个功能时,听这名字就觉得高端,消息闪现-是跳刀blink闪烁躲技能的top10操作吗?可结果让我好失望,哪里有什么闪现的效果,不过是平常的消息 ...
- Flask模板注入
Flask模板注入 Flask模板注入漏洞属于经典的SSTI(服务器模板注入漏洞). Flask案例 一个简单的Flask应用案例: from flask import Flask,render_te ...
- 【C#】组件发布:MessageTip,轻快型消息提示窗
-------------201610212046更新------------- 更新至2.0版,基本完全重写,重点: 改为基于原生LayeredWindow窗体和UpdateLayeredWindo ...
- 一个简单的消息提示jquery插件
最近在工作中写了一个jquery插件,效果如下: 就是一个简单的提示消息的一个东西,支持最大化.最小化.关闭.自定义速度.自定义点击事件,数据有ajax请求和本地数据两种形式.还有不完善的地方,只做了 ...
- Js添加消息提示数量
接到个新需求,类似以下这种需求,得把它封装成一个插件 后端给返回一个这种数据 var data = [ { key:"020506", num:5 }, { key:"0 ...
随机推荐
- shell for 循环数组
name=(aa bb) ;i<${#name[*]};i++)) do name=${name[i]} echo "$name" done
- 【LeetCode每天一题】Combination Sum(组合和)
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- 2019.03.30 Head first
第一节 认识python python.exe -V python 会进入解释器 quit()命令会退出解释器 IDEL,一个python的集成开发环境,能够利用颜色突出语法的编辑器,一个调试工具,P ...
- phpstudy安装redis
php安装扩展,首先要在php官网下载相应的库文件, http://pecl.php.net/package/redis 下载相应版本的文件,首先phpinfo()看看当前的php环境版本等等 我 ...
- cocos2dx JS 层(Layer)的生命周期
场景的生命周期: 一般情况下一个场景只需要一个层,需要创建自己的层类.一些主要的游戏逻辑代码都是写在层中的,场景的生命周期是通过层的生命周期反映出来的,通过重写层的生命周期函数,可以处理场景不同声明周 ...
- gitlab RPM卸载 & 安装 && 升级(9.0.13-》9.5.9-》10.0->10.3.9->10.6.6-》10.8-》11.0)
版本:9.0.3 升级版本:9.0.13 一,停止服务 gitlab-ctl stop unicorn gitlab-ctl stop sidekiq gitlab-ctl stop nginx 二, ...
- undefined reference 问题各种情况分析
扒自网友文章 关于undefined reference这样的问题,大家其实经常会遇到,在此,我以详细地示例给出常见错误的各种原因以及解决方法,希望对初学者有所帮助. 1. 链接时缺失了相关目标文件 ...
- Linux SSH 免秘钥登录
SSH 免秘钥登录 ssh:是一种安全加密协议 ssh username@hostname ssh gongziyuan.com:以当前用户登录该机器(如果不是当前用户,需要这么干:ssh ...
- HttpServletRequest常用方法
1.获取客户机信息 getRequestURL:该方法返回客户端发出请求时的完整URL getRequestURI:该方法返回请求行中的资源名部分 getQueryString:该方法返回请求中的参数 ...
- mysql 5.6 每天凌晨12:00 重置sequence表中的某个值
#.创建evevt要调用的存储过程update_current_value_procedure delimiter // drop procedure if exists update_current ...