码迷,mamicode.com
首页 > 其他好文 > 详细

学习flask---2

时间:2019-10-06 18:49:51      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:secret   应用程序   key   dir   删除cookie   对象   lang   百度首页   脚本   

# request---请求对象
# data----记录请求数据并转换成字符串
# form---表单数据
# args---查询数据
# cookies---cookies信息
# headers---报头信息
# method---请求方式
# url---地址
# files---上传文件

# from flask import *
# app = Flask(__name__)
# @app.route(‘/‘)
# def index():
# print(request.data)
# # print(request.url)
# print(request.args)
# # print(request.method)
# # print(request.form)
# # print(request.headers)
# # print(request.cookies)
# return "首页"
# if __name__ == ‘__main__‘:
# app.run(debug=True)


# 状态保持:http是无状态协议,浏览器请求是无状态
# 无状态的原因:浏览器与服务器使用socket套接字进行通信,服务器请求结束会关闭套接字而且处理页面完毕后会摧毁页面对象.
# cookie---在客户端存储信息
# session ----在服务器存储信息

 


# cookies

#
# from flask import *
# from flask import make_response
# app =Flask(__name__)
# @app.route(‘/‘)
# def index():
# # 展示登陆姓名(n=乃亮,id=01)
# # 从cookie获取信息
# u_n = request.cookies.get(‘name‘)
# u_id = request.cookies.get(‘u_id‘)
# return "%s首页"%u_n
# # 登陆视图函数
# @app.route("/login")
# def login():
# # 表单(登陆表单)---前端代码--
# # 假设---有登陆信息(n=乃亮,id=01)
# # 1,创建cookies
# # 2,写入信息
# # 3,跳转到首页
# # 创建
# response = make_response("qwq")
# # 写入信息
# response.set_cookie("name","乃亮",max_age=3600)
# response.set_cookie("u_id", "888",max_age=3600)
# return response
# # return redirect(url_for(‘index‘))
# #退出函数
# @app.route("/loginout")
# def loginout():
# # 删除cookies的值
# # 创建
# response = make_response("qwq")
# response.delete_cookie("name")
# response.delete_cookie("u_id")
# return response
#
# if __name__ == ‘__main__‘:
# app.run(debug=True,port=8889)

 

 

# session
# 对一些敏感信息,存储在服务器,比如余额用户id验证码.
# session依赖于cookie.
# 上下文:
# 请求上下文,request 封装http请求内容,针对http请求.
# session:用来记录请求会话信息和用户信息.
# 应用上下文:
# 应用上下文主要用来存储应用程序中的变量.
# current_app :
# 应用程序上下文,存储应用程序变量,启动脚本制定参数,加载了那些配置信息,连接的数据库应用在那个机器上运行,IP,内存.
# G变量:
# g最为flask全局临时变量.

# 两者区别:
# 请求上下文:---保存客户端和服务器之间交互数据.
# 应用上下文:flask应用程序运行过程存储的配置信息,应用信息.
# from flask import *
#
# # 创建app对象
# app = Flask(__name__)
# app.config["SECRET_KEY"]="sadasdas"
# # 首页
# @app.route(‘/‘)
# def index():
# # 获取登陆状态
# u_n = session.get("u_name")
# return "%s首页"%u_n
# # 登陆页面
# @app.route(‘/login‘)
# def login():
# session["u_name"]="阿里"
# session["u_id"] ="456"
# return "qwq"
# # 退出页面
# @app.route(‘/out‘)
# def out():
# session.pop("u_name")
# session.pop("u_id")
# return "wqw"
# if __name__ == ‘__main__‘:
# app.run(debug=True,port=8887)


# 视图函数---请求
# 模板----jinja2
# 渲染模板函数----{{变量}}


# 模板

 

 

 

# from flask import *
# app = Flask(__name__)
# @app.route("/")
# def index():
# my_str = "百度首页"
# my_int = 555
# my_li = [1,2,3,5,6,9]
# my_dt ={
# "name":"小李",
# "age":18
# }
# return render_template("index.html",tp_str=my_str,tp_int=my_int,tp_li=my_li,tp_dt=my_dt)
# if __name__ == ‘__main__‘:
# app.run(debug=True,port=8886)

# 绿宝强
# from flask import *
# app = Flask(__name__)
# @app.route("/")
# def index():
# my_dt =[
# {
# "name": "小李",
# },
# {
# "name": "小黑",
# },
# {
# "name": "小王",
# },
# {
# "name": "小吴",
# },
# {
# "name": "宝强",
# },
# ]
# return render_template("index.html",tp_dt=my_dt)
# if __name__ == ‘__main__‘:
# app.run(debug=True,port=8885)


# 过滤器---本质函数
# 过滤器使用方法:---变量名|过滤器名称
# {{data|xxx}}
# 字符串操作
# 小写---lower
# 大写---upper
# 反转---reverse
# 格式化输出 format
# 列表操作
# first取第一个
# Last取最后一个
# length长度
# Sum求和
# sort排序


# 自定义过滤器:
# 过滤器---本质-函数---内置函数
# 通过flask对象进行添加

 

# from flask import *
# app = Flask(__name__)
# # 自定义过滤器函数
# def go_listReverse(li):
# new_li = list(li)
# new_li.reverse()
# return new_li
# app.add_template_filter(go_listReverse,"liReverse")
#
# # 装饰器自定义过滤器
# @app.template_filter("liReverse2")
# def go_listReverse2(li):
# new_li = list(li)
# new_li.reverse()
# return new_li
#
#
# @app.route("/")
# def index():
# my_li = [1, 2, 3, 5, 6, 9]
# return render_template("index.html", tp_li=my_li)
#
# if __name__ == ‘__main__‘:
# app.run(debug=True,port=8885)


# 模板继承---重复使用公共内容,把这些多页面使用到的样板写入父模板
# ,子模板直接继承,不需要重复写代码。

# from flask import *
# app = Flask(__name__)
# @app.route("/")
# def index():
#
# return render_template("demo_index.html")
#
# if __name__ == ‘__main__‘:
# app.run(debug=True,port=8884)


# 拓展---上线测试使用-----下载flask-script包
# from flask import *
# from flask_script import Manager
# app = Flask(__name__)
# # manger类和我们应用程序相关联
# manger = Manager(app)
# @app.route("/")
# def index():
# return "sadsadas"
# if __name__ == ‘__main__‘:
# manger.run()

index页面代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>父页面</title>
</head>
<body>
<p>{{ tp_str }}</p>
<p>{{ tp_li|liReverse2}}</p>
<p>{{ tp_int }}</p>
<p>{{ tp_dt.name }}</p>
<p>{{ tp_dt }}</p>

{% for name in tp_dt if name.name =="宝强" %}
<p style="color: green">{{ name.name }}</p>
{% endfor %}
{% block top %}
<h1>顶部菜单</h1>
{% endblock %}
{% block centent %}
<h1>主题内容</h1>
{% endblock %}
{% block bottom %}
<h1>底部内容</h1>
{% endblock %}
</body>
</html>

 

demo_index页面代码:

{% extends "index.html" %}
{% block centent %}
<h1>主题内容---子页面</h1>
{% endblock %}

 

学习flask---2

标签:secret   应用程序   key   dir   删除cookie   对象   lang   百度首页   脚本   

原文地址:https://www.cnblogs.com/zhangshuntao123/p/11627847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!