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 ...
随机推荐
- Python 全栈开发五 迭代器 生成器 装饰器
一.迭代器 迭代协议:对象必须提供一个next方法,执行该方法后会返回迭代的下一项或者抛出Stopiteration异常,终止迭代.切只能往前,不能倒退. 可迭代对象:遵循迭代写一点对象就是可迭代对象 ...
- [LeetCode] 709. To Lower Case_Easy
Implement function ToLowerCase() that has a string parameter str, and returns the same string in low ...
- conda常用命令
1. conda基本命令 检查Anaconda是否成功安装 conda --version 检测目前安装了哪些环境 conda info --envs 检查目前有哪些版本的python可以安装: co ...
- NserviceBus:消息Message、Command、Event(2)
NServiceBus.IMessage 用于定义消息.NServiceBus.ICommand 用于定义命令.NServiceBus.IEvent 用于定义事件. ICommand 命令 用于点对点 ...
- cocos2d-x JS 随机数
random4 : function (n, m){ var random = Math.floor(Math.random()*(m-n+1)+n); return random;},
- phpstudy的使用
1.第一步是下载phpstudy,你可以百度去下载,也可以通过下面我分享的网盘下载 链接:https://pan.baidu.com/s/1E_CXIrKv1N-jrlA4KCovZA 密码:mkx9 ...
- 网站的title添加图片
将图片作为ico格式,大小设置为16 * 16px左右,太大显示不完整, 命名需为"favicon.ico", 命名需为"favicon.ico", 命名需为& ...
- quartz demo01
1,Pom.xml 加入:quartz-2.1.7.jar <dependency> <groupId>org.quartz-scheduler</groupId&g ...
- Vue系列之 => 全局,私有过滤器
私有过滤器也称局部过滤器 <script> // 全局过滤器 Vue.filter("datatime",function(timestr){ var tm = new ...
- jQuery工具--jQuery.isNumeric(value)和jQuery.trim(str)
jQuery.isNumeric(value) 概述 确定它的参数是否是一个数字. $.isNumeric() 方法检查它的参数是否代表一个数值.如果是这样,它返回 true.否则,它返回false. ...