Learn flask --- 2

Request request object # ---
# ---- Data recorded and converted to request data string
# form form data ---
# --- args query data
# cookies --- cookies information
# header information headers ---
# method --- request method
# url --- address
# files --- upload 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)


# State remains: http is a stateless protocol, the browser requests are stateless
# stateless reason: the browser and server communicate using socket socket, the server end of the request will close the socket and the processing is completed will destroy the page after page objects.
# --- the cookie stored in the client information
# session ---- information stored on a server

 


# cookies

#
# From the Flask Import *
# from the Flask Import make_response
# App = the Flask (__ name__)
# @ app.route ( '/')
# DEF index ():
# # Name Display Log (n = is the light, the above mentioned id = 01)
# # obtain information from the cookie
# U_n = request.cookies.get ( 'name')
# U_ID = request.cookies.get ( 'U_ID')
# return "% S Home" U_n%
# # landed view function
# @ app.route ( "/ the login")
# DEF the login ():
## form (login form) --- front-end code -
## --- assuming that there login information (n = is the light, the above mentioned id = 01)
# # 1, created Cookies
# # 2, written information
# 3 #, goto
# # create
# = make_response the Response ( "qwq")
# # write information
# response.set_cookie ( "name", "is the bright", 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)

 

 

The session #
. # To sensitive information, stored on the server, such as the balance of the user id codes
# depends on the session Cookie.
# Context:
# request context, request content package http request for http requests.
# The session: session to record request information and user information.
# application context:
# application context is mainly used to store application variables.
# current_app:
# application context, to store application variables, startup scripts formulate parameters, load those configuration information, application database connection running on the machine, IP, memory.
# G variable:
# G most flask global temporary variables.

# Difference between the two:
# request context: --- save interaction data between the client and the server.
# Application context: the configuration information storage flask applications running process, the application of information
# * Import from flask
#
# # objects created app
App = the Flask # (__ name__)
# the app.config [ "SECRET_KEY"] = "sadasdas"
# # Home
# @ app.route ( '/')
# DEF index ():
# # get landed status
# u_n = session.get ( "u_name")
# return "% S Home" U_n%
# # landing page
# @ app.route ( '/ the login')
# DEF the login ():
# the session [ "u_name"] = "Ali"
# the session [ " U_ID "] =" 456 "
# return" qwq "
# # quit page
# @ app.route ( '/ OUT')
# DEF OUT ():
# session.pop (" u_name ")
# session.pop ("u_id")
# return "wqw"
# if __name__ == '__main__':
# app.run(debug=True,port=8887)


# --- view function request
# template Jinja2 ----
# ---- rendering template function {{variable}}


Template #

 

 

 

# 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)


# --- nature filter function
# filters use: variable name --- | Filter Name
# {{Data | XXX}}
# string manipulation
# lowercase Lower ---
# --- Upper uppercase
# inversion Reverse ---
# formatted output the format
# operation list
# first take the first
# last taken last
# length length
# sum summation
# sort ordering


Custom filter #:
# Filter Essence --- - --- function built-in function
# flask add the object by

 

# 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)


# Template inheritance --- re-use of public content, these multi-page template to use to write the parent template
#, sub-template directly inherited, do not repeat write code.

# 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)


# Expand on-line test uses ----- --- Download flask-script package
# * Import from the Flask
# Import Manager from flask_script
# App = the Flask (__ name__)
# # Manger class and our associated application
# manger = Manager ( App)
# @ app.route ( "/")
# DEF index ():
# return "sadsadas"
# IF the __name__ == '__main__':
# manger.run ()

index page code:

<!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 == " Po strong"%}
<P style = "Color: Green"> name.name {{}} </ P>
{%} endfor%
{%}% Top Block
<h1> top menu </ h1 of>
{% endblock%}
{% Block centent%}
<h1> subject matter </ h1 of>
{% endblock%}
{% Block bottom%}
<h1> bottom SUMMARY </ h1 of>
{ endblock%}%
</ body>
</ HTML>

 

demo_index page code:

The extends% { "index.html"%}
{%}% Block centent
<h1 of> subpage subject matter --- </ h1 of>
{%}% endblock

 

Guess you like

Origin www.cnblogs.com/zhangshuntao123/p/11627847.html