Flask基础-基础实例
1. 10行代码的迷你程序 flask项目
from flask import Flask
app = Flask(__name__)
@app.route("/index")
def inde():
return "heeello world"
if __name__ =="__main__":
app.run()
2.页面登录的小程序+session简单用法.
from flask import Flask,render_template,request,redirect,session app = Flask(__name__)
app.secret_key="dd" @app.route("/login",methods =["GET","POST"])
def login():
if request.method =='GET': return render_template("login.html") #request.form为post请求
user =request.form.get("user")
pwd =request.form.get("pwd")
if user =="hailong" and pwd =="kuang": #设置session , flask的session存放在cookie当中
session["user_infor"]=user
print(22)
return redirect("/index")
print(111)
return render_template("login.htm",msg ="用户密码错误") @app.route("/index")
def index(): #获取cookie 判断是否存在,如果存在并匹配进行跳转到相关页面
user_infor =session.get("user_infor")
print(222,user_infor)
if not user_infor:
return redirect("/login")
return render_template("index.html") if __name__ =="__main__":
app.run()
login页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title> </head>
</head>
<body>
<h1> 用户登录</h1>
<form method="post">
<input type="text" name ="user">
<input type="password" name ="pwd">
<input type="submit" value="提交">{{msg}} </form>
</body>
</html>
index界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>欢迎来到郝龙江家里</h1>
</body>
</html>


3 .auth认证.
from flask import Flask,render_template,request,redirect,session app = Flask(__name__) app.secret_key = '39jrlasdfoajslfu8af' import functools def auth(func):
@functools.wraps(func) # 保留函数的元信息
def inner(*args,**kwargs):
if not session.get('user_info'):
return redirect('/login')
ret = func(*args,**kwargs)
return ret
return inner @app.route('/login',methods=['GET','POST'])
def login():
if request.method == 'GET':
return render_template('login.html') user = request.form.get('user')
pwd = request.form.get('pwd')
if user == 'hailong' and pwd == 'kuang':
session['user_info'] = user
return redirect('/index')
return render_template('login.html',msg="用户名或密码错误") @app.route('/index')
@auth
def index():
kuang_list = [
{'id':1,'name':'一矿','address':'大同'},
{'id':2,'name':'二矿','address':'大同'},
{'id':3,'name':'三矿','address':'大同'},
] return render_template('index.html',klist=kuang_list)
# return render_template('index.html',**{'klist':kuang_list}) @app.route('/detail')
@auth
def detail():
nid = request.args.get('nid')
return render_template('detail.html') @app.route('/logout')
def logout():
del session['user_info'] return redirect('/login') if __name__ == '__main__':
app.run()
4.中间件

from flask import Flask,render_template,request,redirect,session app = Flask(__name__) app.secret_key = '39jrlasdfoajslfu8af' @app.before_request
def xxxxxxxxxxxxxx():
if request.path == '/login':
return None if not session.get('user_info'):
return redirect('/login') @app.route('/login',methods=['GET','POST'])
def login():
print('我是login')
if request.method == 'GET':
return render_template('login.html') user = request.form.get('user')
pwd = request.form.get('pwd')
if user == 'hailong' and pwd == 'kuang':
session['user_info'] = user
return redirect('/index')
return render_template('login.html',msg="用户名或密码错误") @app.route('/index')
def index():
print('我是index')
kuang_list = [
{'id':1,'name':'一矿','address':'大同'},
{'id':2,'name':'二矿','address':'大同'},
{'id':3,'name':'三矿','address':'大同'},
] return render_template('index.html',klist=kuang_list)
# return render_template('index.html',**{'klist':kuang_list}) @app.route('/detail')
def detail():
nid = request.args.get('nid')
return render_template('detail.html') @app.route('/logout')
def logout():
del session['user_info'] return redirect('/login') if __name__ == '__main__':
app.run()

Flask基础-基础实例的更多相关文章
- 超实用的Flask入门基础教程,新手必备!
Flask入门基础教程 Flask简介 Flask是一个轻量级的可定制框架,使用Python语言编写,较其他同类型框架更为灵活.轻便.安全且容易上手.它可以很好地结合MVC模式进行开发,开发人员分工合 ...
- AutoCAD ObjectARX(VC)开发基础与实例教程2014版光盘镜像
AutoCAD ObjectARX(VC)开发基础与实例教程2014,最新版,光盘镜像 作者:张帆 朱文俊 编著 出版社:中国电力出版社 出版时间:2014年6月 点击一下
- 基础 jQuery 实例
基础 jQuery 实例 jQuery 原则: 由于 jQuery 是为处理 HTML 事件而特别设计的,那么当您遵循以下原则时,您的代码会更恰当且更易维护: 把所有 jQuery 代码置于事件处理函 ...
- 知了课堂 Python Flask零基础 笔记整理
目录 起步 安装Python2.7: Python虚拟环境介绍与安装: pip安装flask: 认识url: URL详解 web服务器和应用服务器以及web应用框架: Flask 第一个flask程序 ...
- expect基础及实例
expect基础及实例 http://blog.csdn.net/zhuying_linux/article/details/6900805
- SVG基础绘图实例
SVG可缩放矢量图(Scalable Vector Graphics),是使用 XML 来描述二维图形和绘图程序的语言,图像在放大或改变尺寸的情况下其图形质量不会有所损失,是万维网联盟的标准. 下面整 ...
- flask之基础概念
[应用]一个 Flask 应用是一个 Flask 类的实例.可以在一个被称为应用工厂的函数内部创建 Flask实例.所有应用相关的配置.注册和其他设置都会在函数内部完成,然后返回这个应用.__init ...
- python基础-基础知识(包括:函数递归等知识)
老男孩 Python 基础知识练习(三) 1.列举布尔值为 False 的值空,None,0, False, ", [], {}, () 2.写函数:根据范围获取其中 3 和 7 整除的所有 ...
- 2、flask之基础知识点
本篇导航: 路由系统 视图函数 请求与响应 模版语法 session 蓝图(blueprint).闪现 (flash) 扩展 一.路由系统 1.可传入参数: @app.route('/user/< ...
随机推荐
- 批量去重URL地址并剔除打不开网址
#coding=utf-8 import os import httplib import socket dictlist ={}; def ReadHost(): hosts = []; obn = ...
- cout<<endl 本质探索
C++中,有一种对象叫操控器(manipulators),专门用来操控stream的对象,在C++标准中,预定义好几种操控器,常见的有: flush 刷新output缓冲区,将内容写入输出设备 end ...
- Mockplus 3.2前瞻,五大特色功能让你惊喜!
在这个火热的夏季,我们有理由热切期待Mockplus 3.2的发布! 作为国产的一流原型设计工具,Mockplus 3.2版本会给我们带来什么呢? 格子(Repeater) 我们平常的设计,有大量需要 ...
- Python自动化面试必备 之 你真明白装饰器么?
Python自动化面试必备 之 你真明白装饰器么? 装饰器是程序开发中经常会用到的一个功能,用好了装饰器,开发效率如虎添翼,所以这也是Python面试中必问的问题,但对于好多小白来讲,这个功能 有点绕 ...
- MUI框架开发HTML5手机APP(一)--搭建第一个手机APP(转)
出处:http://www.cnblogs.com/jerehedu/p/7832808.html 前 言 JRedu 随着HTML5的不断发展,移动开发成为主流趋势!越来越多的公司开始选择使用H ...
- HDU 5956 The Elder (树上斜率DP)
题意:给定上一棵树,然后每条边有一个权值,然后每个点到 1 的距离有两种,第一种是直接回到1,花费是 dist(1, i)^2,还有另一种是先到另一个点 j,然后两从 j 向1走,当然 j 也可以再向 ...
- HDU 2058 The sum problem (数学+暴力)
题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间. 析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的.所以我们就想能 ...
- html 源码 引入样式
post-title2 示例 sdf post-title 示例
- WPF MediaKit的一点问题
原版WPF MediaKit在捕获摄像头视频时,如果不使用640*480分分辨率输出,会出现NewVideoSample事件不被触发的问题. 经数日摸索,终于明白SetVideoCapturePara ...
- 推荐:普通UI设计师与顶级UI设计师的区别是什么?(转)
我不是顶级设计师(我甚至不知道什么才叫顶级),即使见过的一些顶级(知名or优秀)设计师也因为交流不深入,无法评价.但是我勉强可以回答优秀的设计师,和普通的设计师(其实我觉得大部分的普通设计师只是认识他 ...