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 ...
随机推荐
- OpenShift Origin 基本命令
用户管理 $ oc login #登陆$ oc logout #注销$ oc login -u system:admin -n default #以系统管理身份登陆并指定项目$ oc login ht ...
- 2019.03.30 图解HTTP
文章来源<图解HTTP> 第一章 了解Web及网络基础 你有想过当你在浏览器(web browser)的地址栏上输入URL时,Web页面是如何实现的吗? 嗯,好像也没想过 web使用一种名 ...
- gcc 6.2.0/6.3.0/8.2.0 编译安装
参考:http://www.linuxfromscratch.org/blfs/view/stable/general/gcc.html 下载地址在这里:https://ftp.gnu.org/gnu ...
- eclipse签名使用的key文件(android生成keystore)
命令行(或终端)生成keystore文件 在命令行(或终端)输入命令: keytool -genkey -alias Gallery.keystore -keyalg RSA -validit ...
- 转:django模板标签{% for %}的使用(含forloop用法)
django模板标签{% for %}的使用(含forloop用法) {% %}虽然这个是写在html中,但是这里边写的是服务端代码 在django模板标签中,{% for %} 标签用于迭代序列 ...
- unity3d-游戏实战突出重围,第三天 绘制数字
实现效果: 准备资源 using UnityEngine; using System.Collections; public class hznum : MonoBehaviour { //存储图片资 ...
- !! MACD战法总结
我现在只发技术,不预测大盘.其实说实话,大盘不用预测,只要按照guoweijohn战法,有买入信号就入,有卖出信号就出..你也会成为股神..不是吹牛,且听慢慢分解 股市有三种市场: 一.牛市 二.震荡 ...
- 新节点在线加入PXC
环境 192.168.139.151 新增节点 192.168.139.148-150 集群节点 192.168.139.151 已经安装好PXC软件 计划: 选用192.168.139.150 节点 ...
- 008-副文本编辑器UEditor
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- django 设置不带后缀的访问路径
在urls.py 设置空路径,并指向对应的html文件 url(r'^$', views.index),