1|0浏览目录

1|1配置文件

知识点

给你一个路径 “settings.Foo”,可以找到类并获取其中的大写的静态字段。

settings.py

1
2
3
class Foo:
    DEBUG = True
    TEST = True

xx.py  

1
2
3
4
5
6
7
8
9
10
11
12
import importlib
 
path = "settings.Foo"
 
p,c = path.rsplit('.',maxsplit=1)
m = importlib.import_module(p)
cls = getattr(m,c)
 
# 如果找到这个类?
for key in dir(cls):
    if key.isupper():
        print(key,getattr(cls,key))

配置相关

importlib模块

实现机制:根据字符串的形式导入模块,通过反射找到里面的内容,有一个特点,只有全部大写才能被读到。

导入方式:app.config.from_object()

默认配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
flask中的配置文件是一个flask.config.Config对象(继承字典),默认配置为:
    {
        'DEBUG':                                get_debug_flag(default=False),  是否开启Debug模式
        'TESTING':                              False,                          是否开启测试模式
        'PROPAGATE_EXCEPTIONS':                 None,                         
        'PRESERVE_CONTEXT_ON_EXCEPTION':        None,
        'SECRET_KEY':                           None,
        'PERMANENT_SESSION_LIFETIME':           timedelta(days=31),
        'USE_X_SENDFILE':                       False,
        'LOGGER_NAME':                          None,
        'LOGGER_HANDLER_POLICY':               'always',
        'SERVER_NAME':                          None,
        'APPLICATION_ROOT':                     None,
        'SESSION_COOKIE_NAME':                  'session',
        'SESSION_COOKIE_DOMAIN':                None,
        'SESSION_COOKIE_PATH':                  None,
        'SESSION_COOKIE_HTTPONLY':              True,
        'SESSION_COOKIE_SECURE':                False,
        'SESSION_REFRESH_EACH_REQUEST':         True,
        'MAX_CONTENT_LENGTH':                   None,
        'SEND_FILE_MAX_AGE_DEFAULT':            timedelta(hours=12),
        'TRAP_BAD_REQUEST_ERRORS':              False,
        'TRAP_HTTP_EXCEPTIONS':                 False,
        'EXPLAIN_TEMPLATE_LOADING':             False,
        'PREFERRED_URL_SCHEME':                 'http',
        'JSON_AS_ASCII':                        True,
        'JSON_SORT_KEYS':                       True,
        'JSONIFY_PRETTYPRINT_REGULAR':          True,
        'JSONIFY_MIMETYPE':                     'application/json',
        'TEMPLATES_AUTO_RELOAD':                None,
    }

修改配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
方式一:
    app.config['DEBUG'] = True
  
    PS: 由于Config对象本质上是字典,所以还可以使用app.config.update(...)
  
方式二:
    app.config.from_pyfile("python文件名称")
        如:
            settings.py
                DEBUG = True
  
            app.config.from_pyfile("settings.py")
  
    app.config.from_envvar("环境变量名称")
        环境变量的值为python文件名称名称,内部调用from_pyfile方法
  
  
    app.config.from_json("json文件名称")
        JSON文件名称,必须是json格式,因为内部会执行json.loads
  
    app.config.from_mapping({'DEBUG':True})
        字典格式
  
    app.config.from_object("python类或类的路径")
  
    如:app.config.from_object('pro_flask.settings.TestingConfig')
  
        settings.py
  
            class Config(object):
                DEBUG = False
                TESTING = False
                DATABASE_URI = 'sqlite://:memory:'
  
            class ProductionConfig(Config):
                DATABASE_URI = 'mysql://user@localhost/foo'
  
            class DevelopmentConfig(Config):
                DEBUG = True
  
            class TestingConfig(Config):
                TESTING = True
  
        PS: 从sys.path中已经存在路径开始写
      
  
    PS: settings.py文件默认路径要放在程序root_path目录,如果instance_relative_config为True,则就是instance_path目录

常用:app.config.from_object()

1|2路由系统

本质:带参数的装饰器和闭包实现的。

  • @app.route('/user/<username>')

  • @app.route('/post/<int:post_id>')

  • @app.route('/post/<float:post_id>')

  • @app.route('/post/<path:path>')

  • @app.route('/login', methods=['GET', 'POST'])

常用路由系统有以上五种,所有的路由系统都是基于一下对应关系来处理:

1
2
3
4
5
6
7
8
9
DEFAULT_CONVERTERS = {
    'default':          UnicodeConverter,
    'string':           UnicodeConverter,
    'any':              AnyConverter,
    'path':             PathConverter,
    'int':              IntegerConverter,
    'float':            FloatConverter,
    'uuid':             UUIDConverter,
}

总结:

  • app.route()支持三个参数:url、method、endpoint(相当于django中的name);
  • endpoint:反向生成url,如果不写,默认为函数名,用url_for 反向生成url
  • 动态路由:一定要有视图函数接收。如果显示列表页面,需要传id,就要有一个地方接收。app.route('/post/<int:id>'),如上五种方法。限制参数类型。注意:它只支持这几种类型,不支持正则表达式。
  • 如果带参数,如何反向生成:
1
2
3
4
@app.route('/index/<int:nid>',methods=["GET","POST"])
def index(nid):
    url_for("index",nid=1#/index/1
    return "Index"
  • 没有参数:url_for('endpoint'),有参数:url_for("index",nid=777)

1|3视图

FBV

1|4请求相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 请求相关信息
request.method
request.args
request.form
request.values
request.cookies
request.headers
request.path
request.full_path
request.script_root
request.url
request.base_url
request.url_root
request.host_url
request.host
request.files        #上传文件 
obj = request.files['the_file_name']    #拿到上传的文件名
obj.save('/var/www/uploads/' + secure_filename(f.filename)) #将文件保存,写到本地的路径

1|5响应

1
2
3
4
5
# 响应相关信息
return "字符串"
return jsonify({'k1':'v1'})  #内部帮我们序列化
return render_template('html模板路径',**{})
return redirect('/index.html')     #重定向

注:jsonify内部帮我们序列化,跟django的jsonresponse有点相似。 

我们返回的都是响应体,页面可以看到的。那我们如何加响应头? 

如果我们想要返回相关的信息时,可以通过make_response将我们的内容封装起来。

1
2
3
4
response = make_response(render_template('index.html'))
# response是flask.wrappers.Response类型
response.headers['X-Something'] = 'A value'  #加响应头
return response

那我们可以设置cookie吗?

1
2
3
4
5
6
response = make_response(render_template('index.html'))
# response是flask.wrappers.Response类型
response.delete_cookie('key')
response.set_cookie('key', 'value')
 
return response  

1|6模板渲染 

1、模板的使用

Flask使用的是Jinja2模板,所以其语法和Django无差别

2、自定义模板方法

Flask中自定义模板方法的方式和Bottle相似,创建一个函数并通过参数的形式传入render_template,如:

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <h1>自定义函数</h1>
  9. {{ww()|safe}}
  10. </body>

  11. </html>
  12.  
  13.  
  14.  

HTML

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. from flask import Flask,render_template
  4. app = Flask(__name__)
  5. def wupeiqi():

  6. return '<h1>yaya</h1>'
  7. @app.route('/login', methods=['GET', 'POST'])

  8. def login():

  9. return render_template('login.html', ww=yaya)
  10. app.run()
  11.  
  12.  
  13.  

run.py

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. {</span>% macro input(name, type=<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">text</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span>, value=<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">''</span>) %<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">}
  9.     </span>&lt;input type=<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">{{ type }}</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span> name=<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">{{ name }}</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span> value=<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">{{ value }}</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span>&gt;<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">
  10. {</span>% endmacro %<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">}
  11. {{ input(</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">n1</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">) }}
  12. {</span>% include <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">tp.html</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span> %<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">}
  13. </span>&lt;h1&gt;asdf{{ v.k1}}&lt;/h1&gt;
  14. </body>

  15. </html>

  16.  
  17.  
  18.  

其他

注意:Markup等价django的mark_safe

模板详细用法

基本数据类型

可以执行python语法,如:dict.get(),list["xx"]

传入函数

Django中函数自动加括号执行;

Flask中不自动执行,需要自己主动执行,可以传参数。

全局定义函数

1
2
3
4
5
6
7
8
9
@app.template_global()
def sb(a1, a2):
    # {{sb(1,9)}}
    return a1 + a2
 
@app.template_filter()
def db(a1, a2, a3):
    # {{ 1|db(2,3) }}
    return a1 + a2 + a3

模板继承 

layout.html

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <h1>模板</h1>
    {% block content %}{% endblock %}
</body>
</html>

tpl.html

1
2
3
4
5
6
7
{% extends "layout.html"%}
 
{% block content %}
    {{users.0}}
                     
 
{% endblock %} 

include  

1
2
3
4
5
6
7
8
{% include "form.html" %}
 
form.html
    <form>
        asdfasdf
        asdfasdf
         
    </form>

宏 

1
2
3
4
5
{% macro ccccc(name, type='text', value='') %}
    <h1>宏</h1>
    <input type="{{ type }}" name="{{ name }}" value="{{ value }}">
    <input type="submit" value="提交">
{% endmacro %}

#默认不显示,相当于定义了函数没执行,想要执行,需要调用

要用几次,就调用几遍  

1
2
3
{{ ccccc('n1') }}
 
{{ ccccc('n2') }}  

安全

前端做法 

1
{{u|safe}}

后端做法

1
MarkUp("asdf")  

注:Flask中的markup相当于Django中的mark_safe. 

1|7session

除请求对象之外,还有一个 session 对象。它允许你在不同请求间存储特定用户的信息。它是在 Cookies 的基础上实现的,并且对 Cookies 进行密钥签名要使用会话,你需要设置一个密钥。

  • 设置:session['username'] = 'xxx'

  • 删除:session.pop('username', None)
  1. from flask import Flask, session, redirect, url_for, escape, request
  2. app = Flask(name)
  3. @app.route('/')

  4. def index():

  5. if 'username' in session:

  6. return 'Logged in as %s' % escape(session['username'])

  7. return 'You are not logged in'
  8. @app.route('/login', methods=['GET', 'POST'])

  9. def login():

  10. if request.method == 'POST':

  11. session['username'] = request.form['username']

  12. return redirect(url_for('index'))

  13. return '''

  14. <form action="" method="post">

  15. <p><input type=text name=username>

  16. <p><input type=submit value=Login>

  17. </form>

  18. '''
  19. @app.route('/logout')

  20. def logout():

  21. # remove the username from the session if it's there

  22. session.pop('username', None)

  23. return redirect(url_for('index'))
  24. # set the secret key. keep this really secret:

  25. app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
  26.  
  27.  
  28.  

基本使用

  1. pip3 install Flask-Session
  2.     run.py
  3.         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> flask <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> Flask
  4.         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> flask <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> session
  5.         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> pro_flask.utils.session <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> MySessionInterface
  6.         app </span>= Flask(<span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__name__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">)
  7.         app.secret_key </span>= <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">A0Zr98j/3yX R~XHH!jmN]LWX/,?RT</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">
  8.         app.session_interface </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> MySessionInterface()
  9.         @app.route(</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">/login.html</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span>, methods=[<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">GET</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span>, <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">POST</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">])
  10.         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> login():
  11.             </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">print</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(session)
  12.             session[</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">user1</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span>] = <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">alex</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">
  13.             session[</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">user2</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span>] = <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">alex</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span>
  14.             <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">del</span> session[<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">user2</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">]
  15.             </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">内容</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span>
  16.         <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">if</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__name__</span> == <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">__main__</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">:
  17.             app.run()
  18.     session.py
  19.         </span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">!/usr/bin/env python</span>
  20.         <span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> -*- coding:utf-8 -*-</span>
  21.         <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> uuid
  22.         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> json
  23.         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> flask.sessions <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> SessionInterface
  24.         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> flask.sessions <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> SessionMixin
  25.         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> itsdangerous <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> Signer, BadSignature, want_bytes
  26.         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">class</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> MySession(dict, SessionMixin):
  27.             </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__init__</span>(self, initial=None, sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">None):
  28.                 self.sid </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> sid
  29.                 self.initial </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> initial
  30.                 super(MySession, self).</span><span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__init__</span>(initial <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">or</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> ())
  31.             </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__setitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(self, key, value):
  32.                 super(MySession, self).</span><span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__setitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(key, value)
  33.             </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__getitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(self, item):
  34.                 </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> super(MySession, self).<span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__getitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(item)
  35.             </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__delitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(self, key):
  36.                 super(MySession, self).</span><span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__delitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(key)
  37.         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">class</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> MySessionInterface(SessionInterface):
  38.             session_class </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> MySession
  39.             container </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> {}
  40.             </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__init__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(self):
  41.                 </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> redis
  42.                 self.redis </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> redis.Redis()
  43.             </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> _generate_sid(self):
  44.                 </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> str(uuid.uuid4())
  45.             </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> _get_signer(self, app):
  46.                 </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">if</span> <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">not</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> app.secret_key:
  47.                     </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> None
  48.                 </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> Signer(app.secret_key, salt=<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">flask-session</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">,
  49.                               key_derivation</span>=<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">hmac</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">)
  50.             </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> open_session(self, app, request):
  51.                 </span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"""</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">
  52.                 程序刚启动时执行,需要返回一个session对象
  53.                 </span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"""</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">
  54.                 sid </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> request.cookies.get(app.session_cookie_name)
  55.                 </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">if</span> <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">not</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> sid:
  56.                     sid </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self._generate_sid()
  57.                     </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> self.session_class(sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">sid)
  58.                 signer </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self._get_signer(app)
  59.                 </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">try</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">:
  60.                     sid_as_bytes </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> signer.unsign(sid)
  61.                     sid </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> sid_as_bytes.decode()
  62.                 </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">except</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> BadSignature:
  63.                     sid </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self._generate_sid()
  64.                     </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> self.session_class(sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">sid)
  65.                 </span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> session保存在redis中</span>
  66.                 <span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> val = self.redis.get(sid)</span>
  67.                 <span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> session保存在内存中</span>
  68.                 val =<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.container.get(sid)
  69.                 </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">if</span> val <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">is</span> <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">not</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> None:
  70.                     </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">try</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">:
  71.                         data </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> json.loads(val)
  72.                         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> self.session_class(data, sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">sid)
  73.                     </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">except</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">:
  74.                         </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> self.session_class(sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">sid)
  75.                 </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> self.session_class(sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">sid)
  76.             </span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> save_session(self, app, session, response):
  77.                 </span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"""</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">
  78.                 程序结束前执行,可以保存session中所有的值
  79.                 如:
  80.                     保存到resit
  81.                     写入到用户cookie
  82.                 </span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"""</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">
  83.                 domain </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.get_cookie_domain(app)
  84.                 path </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.get_cookie_path(app)
  85.                 httponly </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.get_cookie_httponly(app)
  86.                 secure </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.get_cookie_secure(app)
  87.                 expires </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.get_expiration_time(app, session)
  88.                 val </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> json.dumps(dict(session))
  89.                 </span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> session保存在redis中</span>
  90.                 <span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> self.redis.setex(name=session.sid, value=val, time=app.permanent_session_lifetime)</span>
  91.                 <span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> session保存在内存中</span>
  92. self.container.setdefault(session.sid, val)

  93.                 session_id </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self._get_signer(app).sign(want_bytes(session.sid))
  94.                 response.set_cookie(app.session_cookie_name, session_id,
  95.                                     expires</span>=expires, httponly=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">httponly,
  96.                                     domain</span>=domain, path=path, secure=secure)</div></div><div id="mCSB_5_scrollbar_vertical" class="mCSB_scrollTools mCSB_5_scrollbar mCS-minimal-dark mCSB_scrollTools_vertical" style="display: none;"><div class="mCSB_draggerContainer"><div id="mCSB_5_dragger_vertical" class="mCSB_dragger" style="position: absolute; min-height: 50px; top: 0px;"><div class="mCSB_dragger_bar" style="line-height: 50px;"></div></div><div class="mCSB_draggerRail"></div></div></div><div id="mCSB_5_scrollbar_horizontal" class="mCSB_scrollTools mCSB_5_scrollbar mCS-minimal-dark mCSB_scrollTools_horizontal" style="display: block;"><div class="mCSB_draggerContainer"><div id="mCSB_5_dragger_horizontal" class="mCSB_dragger" style="position: absolute; min-width: 50px; display: block; width: 0px; left: 0px;"><div class="mCSB_dragger_bar"></div></div><div class="mCSB_draggerRail"></div></div></div></pre>
  97. 自定义session

  98. #!/usr/bin/env python
  99. # -*- coding:utf-8 -*-
  100. """
  101. pip3 install redis
  102. pip3 install flask-session
  103. """
  104. from flask import Flask, session, redirect

  105. from flask.ext.session import Session
  106. app = Flask(name)

  107. app.debug = True

  108. app.secret_key = 'asdfasdfasd'
  109. app.config['SESSION_TYPE'] = 'redis'

  110. from redis import Redis

  111. app.config['SESSION_REDIS'] = Redis(host='192.168.0.94',port='6379')

  112. Session(app)
  113. @app.route('/login')

  114. def login():

  115. session['username'] = 'alex'

  116. return redirect('/index')
  117. @app.route('/index')

  118. def index():

  119. name = session['username']

  120. return name
  121. if name == 'main':

  122. app.run()
  123. 第三方session

  124. 总结

  125. 以加密的形式放到浏览器的cookie里面。

  126. 用户浏览器可以禁用cookie,禁用掉之后就不能用。用户登录就不能成功。

  127. 请求进来去cookie中把数据拿到,拿到之后将数据解密并反序列化成字典放到内存,让视图函数使用,视图函数使用完交给其他人,再进行序列化加密放到session中去,

  128. 本质:放到session,再给他移除掉。

  129. 当请求刚到来:flask读取cookie中session对应的值:eyJrMiI6NDU2LCJ1c2VyIjoib2xkYm95,将该值解密并反序列化成字典,放入内存以便视图函数使用。
    视图函数:

  130. 1
  131. 2
  132. 3
  133. 4
  134. 5
  135. 6
  136. 7
  137. @app.route('/ses')
  138. def ses():
  139.     session['k1'] = 123
  140.     session['k2'] = 456
  141.     del session['k1']
  142.  
  143.     return "Session"           
  144.  
  145. session是以字典的形式保存在cookie中,字典有啥操作,它就有啥操作。

  146. 当请求结束时,flask会读取内存中字典的值,进行序列化+加密,写入到用户cookie中。

  147. 可以在配置文件中对相应的配置进行修改。

  148. 生命周期默认是31天,可以修改。Django中cookie生命周期默认是2周。

  149. 1|8闪现

  150.  
  151. 是一个基于Session实现的用于保存数据的集合,其特点是:使用一次就删除。

  152. 在session中存储一个数据,读取时通过pop将数据移除。

  153. 1
  154. 2
  155. 3
  156. 4
  157. 5
  158. 6
  159. 7
  160. 8
  161. 9
  162. 10
  163. 11
  164. 12
  165. 13
  166. 14
  167. from flask import Flask,flash,get_flashed_messages
  168. @app.route('/page1')
  169. def page1():
  170.  
  171.     flash('临时数据存储','error')
  172.     flash('sdfsdf234234','error')
  173.     flash('adasdfasdf','info')
  174.  
  175.     return "Session"
  176.  
  177. @app.route('/page2')
  178. def page2():
  179.     print(get_flashed_messages(category_filter=['error']))
  180.     return "Session"  
  181.  
  182. 1|9中间件

  183.  
  184. 请求执行wsgi.app之前和之后定制一些操作,用的是call方法。

  185. Django和Falsk请求源码的入口就是call方法。

  186. call方法什么时候触发?

  187. 用户发起请求时,才执行。

  188. 任务

  189. 在执行call方法之前,做一个操作,call方法执行之后做一个操作。

  190. 方式一:改源码

  191. 方式二:

  192. 1
  193. 2
  194. 3
  195. 4
  196. 5
  197. 6
  198. 7
  199. 8
  200. 9
  201. 10
  202. 11
  203. 12
  204. class Middleware(object):
  205.     def __init__(self,old):
  206.         self.old = old
  207.  
  208.     def __call__(self, *args, **kwargs):  #4、浏览器发送请求,触发__call__方法
  209.         ret = self.old(*args, **kwargs)  #5、赋值
  210.         return ret
  211.  
  212.  
  213. if __name__ == '__main__':     #1
  214.     app.wsgi_app = Middleware(app.wsgi_app)  #2、先会给app.wsgi_app赋值
  215.     app.run()     #3、启动werkzeug服务器,等待请求
  216.  
  217. Django中的中间件是请求和响应时做一些操作,而Flask中间件是自定义一些操作。在请求执行之前和执行之后定制一些操作。

  218. Flask源码入口:

  219.   

  220. 1|10蓝图(blueprint)

  221.  
  222. 目标

  223. 给开发者提供目录结构

  224. 简单蓝图步骤:

    • 创建一个跟项目名同名的目录。
    • 在跟项目名同名的目录下新建__init__.py
      • 新建函数下实例化app
    • 在项目下新建manage.py
      • 导入app
    • 在目录下创建views文件夹,放所有的视图,根据不同的业务建相应的py文件
    • 在视图py文件中新建蓝图,并实例化蓝图对象
    • 在__init__.py文件中导入蓝图并注册
    • 在目录下新建templates文件夹,放所有的模板
    • 在目录下新建static文件夹,放所有的静态文件
  225. 总结:

    • 目录结构的划分;
    • 前缀;
    • 特殊装饰器;
  226. 其他

  227. 1、如果某一个蓝图想在别的地方找模板,怎么办?

  228. 如上所示,在实例化蓝图中可以自定义模板位置。那蓝图如果想用模板怎么找?

  229. 先找目录下的template下的,没有才去蓝图中找。跟Django一样。(先在项目中找,没找到再去app中去找。)

  230. 2、还可以给某一类加前缀。

  231. 在每次执行前都要加上前缀,否则报错。

  232. 3、可以给某一类添加before_request

  233. 1
  234. 2
  235. 3
  236. 4
  237. 5
  238. 6
  239. 7
  240. 8
  241. 9
  242. 10
  243. 11
  244. 12
  245. 13
  246. 14
  247. 15
  248. 16
  249. from flask import Blueprint,render_template
  250.  
  251. ac = Blueprint('ac',__name__,template_folder="xxxxx")
  252.  
  253. @ac.before_request
  254. def x1():
  255.     print('app.before_request')
  256.  
  257. @ac.route('/login')
  258. def login():
  259.     return render_template('login.html')
  260.  
  261.  
  262. @ac.route('/logout')
  263. def logout():
  264.     return 'Logout'
  265.  
  266. 这个什么时候用到呢?

  267. 登录认证。

  268. 只要登录成功才能访问的蓝图中就加before_request. 

  269. 1|11特殊装饰器

  270.  
  271. before_request

    • 不需要加参数
    • 没有返回值
    • 谁先定义谁先执行(Django框架中间件的process_request)
  272. after_request

    • 需要至少加一个参数
    • 要有返回值
    • 谁后定义谁先执行(Django框架中间件的process_response)(内部反转了一下)
  273. flask与django1.9版本之前,不管请求函数有没有return,中间件响应函数都执行。

  274. 1
  275. 2
  276. 3
  277. 4
  278. 5
  279. 6
  280. 7
  281. 8
  282. 9
  283. 10
  284. 11
  285. 12
  286. 13
  287. 14
  288. 15
  289. 16
  290. 17
  291. 18
  292. 19
  293. 20
  294. 21
  295. 22
  296. 23
  297. 24
  298. 25
  299. 26
  300. 27
  301. 28
  302. 29
  303. 30
  304. 31
  305. 32
  306. 33
  307. 34
  308. 35
  309. 36
  310. 37
  311. 38
  312. 39
  313. 40
  314. 41
  315. from flask import Flask
  316. app = Flask(__name__)
  317.  
  318.  
  319. @app.before_request
  320. def x1():
  321.     print('before:x1')
  322.     return ''
  323.  
  324. @app.before_request
  325. def xx1():
  326.     print('before:xx1')
  327.  
  328.  
  329. @app.after_request
  330. def x2(response):
  331.     print('after:x2')
  332.     return response
  333.  
  334. @app.after_request
  335. def xx2(response):
  336.     print('after:xx2')
  337.     return response
  338.  
  339.  
  340.  
  341. @app.route('/index')
  342. def index():
  343.     print('index')
  344.     return "Index"
  345.  
  346.  
  347. @app.route('/order')
  348. def order():
  349.     print('order')
  350.     return "order"
  351.  
  352.  
  353. if __name__ == '__main__':
  354.  
  355.     app.run()
  356.  
  357. before_first_request  

  358. 项目启动起来,第一次请求才执行。

  359. 是一个标识,最开始是True,第一次请求之后改为False,就不再执行。

  360. 1
  361. 2
  362. 3
  363. 4
  364. 5
  365. 6
  366. 7
  367. 8
  368. 9
  369. 10
  370. 11
  371. 12
  372. 13
  373. 14
  374. 15
  375. 16
  376. 17
  377. 18
  378. 19
  379. 20
  380. 21
  381. 22
  382. 23
  383. from flask import Flask
  384. app = Flask(__name__)
  385.  
  386. @app.before_first_request
  387. def x1():
  388.     print('123123')
  389.  
  390.  
  391. @app.route('/index')
  392. def index():
  393.     print('index')
  394.     return "Index"
  395.  
  396.  
  397. @app.route('/order')
  398. def order():
  399.     print('order')
  400.     return "order"
  401.  
  402.  
  403. if __name__ == '__main__':
  404.  
  405.     app.run()
  406.  
  407. template_global

  408. 给模板用

  409. 1
  410. 2
  411. 3
  412. 4
  413. @app.template_global()
  414. def sb(a1, a2):
  415.     # {{sb(1,9)}}
  416.     return a1 + a2
  417.  
  418. template_filter

  419. 给模板用

  420. 1
  421. 2
  422. 3
  423. 4
  424. @app.template_filter()
  425. def db(a1, a2, a3):
  426.     # {{ 1|db(2,3) }}
  427.     return a1 + a2 + a3
  428.  
  429. errorhandler

  430. 定制错误页面

  431. 1
  432. 2
  433. 3
  434. 4
  435. @app.errorhandler(404)
  436. def not_found(arg):
  437.     print(arg)
  438.     return "没找到"
  439.  
  440.   

  441.   

  442. __EOF__

  443. 作  者:高雅
    出  处:https://www.cnblogs.com/gaoya666/p/9174665.html
    关于博主:编程路上的小学生,热爱技术,喜欢专研。评论和私信会在第一时间回复。或者直接私信我。
    版权声明:署名 - 非商业性使用 - 禁止演绎,协议普通文本 | 协议法律文本
    声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!

posted @
2019-12-05 18:57 
ABDM 
阅读(...) 
评论(...) 
编辑 
收藏

Flask框架之功能详解的更多相关文章

  1. Flask框架 之 功能详解

    浏览目录 配置文件 路由系统 视图 请求相关 响应 模板渲染 session 闪现 中间件 蓝图(blueprint) 特殊装饰器 配置文件 知识点 给你一个路径 “settings.Foo”,可以找 ...

  2. iOS-----AVFoundation框架的功能详解

    使用AVFoundation拍照和录制视频 需要开发自定义的拍照和录制视频功能,可借助于AVFoundation框架来实现,该框架提供了大量的类来完成拍照和录制视频.主要使用如下类: AVCaptur ...

  3. VideoPipe可视化视频结构化框架新增功能详解(2022-11-4)

    VideoPipe从国庆节上线源代码到现在经历过了一个月时间,期间吸引了若干小伙伴的参与,现将本阶段新增内容总结如下,有兴趣的朋友可以加微信拉群交流. 项目地址:https://github.com/ ...

  4. .NET ORM框架 SqlSuagr4.0 功能详解与实践【开源】

    SqlSugar 4.0 ORM框架的优势 为了未来能够更好的支持多库分布式的存储,并行计算等功能,将SqlSugar3.x全部重写,现有的架构可以轻松扩展多库. 源码下载: https://gith ...

  5. java的集合框架最全详解

    java的集合框架最全详解(图) 前言:数据结构对程序设计有着深远的影响,在面向过程的C语言中,数据库结构用struct来描述,而在面向对象的编程中,数据结构是用类来描述的,并且包含有对该数据结构操作 ...

  6. iOS之UI--使用SWRevealViewController实现侧边菜单功能详解实例

    使用SWRevealViewController实现侧边菜单功能详解 下面通过两种方法详解SWRevealViewController实现侧边菜单功能: 1.使用StoryBoard实现   2.纯代 ...

  7. 转载]IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本 )

    原文地址:IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本作者:佐佐木小次郎 因为最近项目上要用有关LBS的功能.于是我便做一下预研. 一般说来LBS功能一般分为两块:一块是地理 ...

  8. SNS社交系统“ThinkSNS V4.6”活动应用功能详解及应用场景举例

    sns社交系统ThinkSNS目前拥有功能:朋友圈(微博).微吧(论坛).频道.积分商城.IM即时聊天.直播.问答.活动.资讯(CMS).商城.广场.找人.搜索.评论.点赞.转发.分享.话题.积分.充 ...

  9. Struts功能详解——ActionMapping对象

    Struts功能详解——ActionMapping对象 ActionMapping描述了struts中用户请求路径和Action的映射关系,在struts中每个ActionMapping都是通过pat ...

随机推荐

  1. Eclipse查看git中的历史,显示详细时间

    clipse show date details in git history我的eclipse查看git history,显示为相对时间,并不是很方便,想要查看某个具体日期的版本代码,就需要设置为具 ...

  2. Django views 中的装饰器

    关于装饰器 示例: 有返回值的装饰器:判断用户是否登录,如果登录继续执行函数,否则跳回登录界面 def auth(func): def inner(request, *args, **kwargs): ...

  3. matlab练习程序(DBSCAN)

    DBSCAN全称Density-Based Spatial Clustering of Applications with Noise,是一种密度聚类算法. 和Kmeans相比,不需要事先知道数据的类 ...

  4. Java 基础系列:不变性

    1.1 定义 不可变类(Immutable Objects):当类的实例一经创建,其内容便不可改变,即无法修改其成员变量. 可变类(Mutable Objects):类的实例创建后,可以修改其内容. ...

  5. 第三节: List类型的介绍、生产者消费者模式、发布订阅模式

    一. List类型基础 1.介绍 它是一个双向链表,支持左进.左出.右进.右出,所以它即可以充当队列使用,也可以充当栈使用. (1). 队列:先进先出, 可以利用List左进右出,或者右进左出(Lis ...

  6. Expression Tree上手指南 (一)【转】

    大家可能都知道Expression Tree是.NET 3.5引入的新增功能.不少朋友们已经听说过这一特性,但还没来得及了解.看看博客园里的老赵等诸多牛人,将Expression Tree玩得眼花缭乱 ...

  7. 生成 Visual Studio 中的代码的文档生成神器

    当我们在团队开发中的时候,经常要给别人提供文档,有了这个工具,设置一下,一键生成.前提是你要写好xml注释. 这也是开源项目: https://sandcastle.codeplex.com/ 它就是 ...

  8. 联合 CNCF 共同出品:Kubernetes and Cloud Native Meetup 成都站

    亮点解读 云原生前沿技术分享:阿里经济体“云原生化”宝贵经验与最佳实践成果 OpenKruise 价值几何? 防踩坑指南:国内知名容器平台架构师解读从 ECS 迁移到 K8S 走过哪些坑. ​云原生服 ...

  9. JQ动态生成节点绑定事件无效问题

    最近做项目的时候遇见了一个问题,通过jq将动态节点绑定到dom节点上,并且为动态节点绑定方法,此方法再次为动态节点添加动态节点,但在刷新之后,动态节点上的方法失效了,过程为:创建动态节点->动态 ...

  10. WPF数据模板(7)

    数据模板常用在3种类型的控件, 下图形式: 1.Grid这种列表表格中修改Cell的数据格式, CellTemplate可以修改单元格的展示数据的方式. 2.针对列表类型的控件, 例如树形控件,下拉列 ...