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 ...
随机推荐
- 【LeetCode每天一题】Pascal's Triangle(杨辉三角)
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- CLR总览
Contents 第1章CLR的执行模型... 4 1.1将源代码编译成托管代码模块... 4 1.2 将托管模块合并成程序集... 6 1.3加载公共语言运行时... 7 1.4执行程序集的代码.. ...
- [Python] Frequently used method or solutions for issues
Web Scraping爬虫 for Mac urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] cer ...
- 23个适合logo设计的常用英文字体
在很多国外的品牌中我们都会发现他们的英文字体在logo的运用中,不仅会提升logo的品质还会让logo看起来更加美观.今天我们就来看看都有哪些常常出现在logo设计中的英文字体吧. 字体,与文字本 ...
- winrar自解压释放路径详解
因为在 WINRAR 的帮助文件中没有对自解压路径和系统的环境变量之间作说明,所以,很多人只知道,其自解压路径可以智能定位到系统的 PROGRAM FILES 目录,而不知道它其实还可以智能定位到系统 ...
- leetcode473 Matchsticks to Square
一开始想求所有结果为target的组合来着,但是所选元素不能重叠.用这个递归思想很简单,分成四个桶,每次把元素放在任意一个桶里面,最后如果四个桶相等就可以放进去,有一个地方可以剪枝,假如任意一个桶的元 ...
- python string method
嗯,学习其它语言没这样全练过,嘻嘻 //test.py 1 # -*- coding: UTF-8 -*- 2 3 str = "i am worker" 4 print str. ...
- Go cookie
Web状态,对于我们从c/c++转过来的人来说还是很重视的啊 但,如何用好cookie来让我心顺畅,目前还是有点障碍 可能是我没能完全理解cookie 但是,如果由浏览器客户端决定自己绑定那个cook ...
- Django Form(表单)
前台用 get 或 post 方法向后台提交一些数据. GET方法: html示例(保存在templates文件夹中): <!DOCTYPE html> <html> < ...
- 原生js---ajax---post方法传数据
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...