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 ...
随机推荐
- k8s 容器的生命周期钩子
钩子有两个一个容器起之前定义一个动作PostStart,容器关闭之前定义一个动作PreStop 动作可以是一个命令或http请求 示例 spec: containers: - lifecycle: p ...
- js 图片区域可点击,适配移动端,图片大小随意改变
实现图片区域可点击,实际上使用map是可以的,但是适配效果并不好,图片只能是固定大小的值,而且点都被写死了. 在这里,我使用的js基于canvas写的一个小工具.可以圈出你需要点击的部分,然后生成一串 ...
- Struts2漏洞利用工具下载(更新2017-V1.8版增加S2-045/S2-046)
Struts2漏洞利用工具下载(已更新V1.8版) 2017-03-21:增加S2-046,官方发布S2-046和S2-045漏洞引发原因一样,只是利用漏洞的位置发生了变化,S2-046方式可能绕过部 ...
- go https json
好吧,再来一个看起来高档点的吧 自从知道 Go有本地调用后,我就回到windows了 哈哈,以下内容,均在win10下搞定 预备:先做两个文件,服务器端的私钥KEY和公钥证书 1. openssl g ...
- iOS UI基础-12.0 Storyboard
storyboard创建控制器 1.先加载storyboard文件(Test是storyboard的文件名) UIStoryboard *storyboard = [UIStoryboard stor ...
- Hibernate框架的第四天
## Hibernate框架的第四天 ## ---------- **回顾:Hibernate框架的第三天** 1. 一对多关联关系映射 * JavaBean的编写 * 编写映射的配置文件 * 使用级 ...
- DoTween
dotween最原始的用法 using System.Collections; using System.Collections.Generic; using UnityEngine; using D ...
- laravel中使用event
https://www.cnblogs.com/ziyouchutuwenwu/p/4274539.html
- 一个获取本机ip地址的正则
ifconfig|grep -oP '(?<=inet addr:)(?=(?!127\.0\.0\.1))\d+(\.\d+){3}'
- c#关于字符串格式化
1. 如何使用文化来格式化日期 如: /// <summary> /// 根据语言获取文化名称 /// </summary> /// <returns></r ...