互联网服务供应商

aiohttp-flashbag的Python项目详细描述


库为aiohttp.web提供flashbag。

https://img.shields.io/travis/aio-libs/aiohttp-flashbag.svghttps://codecov.io/github/aio-libs/aiohttp-flashbag/coverage.svg

用法

库允许我们在会话内的请求之间共享一些数据。

基本用法示例:

importaiohttp_flashbagfromaiohttpimportwebfromaiohttp_sessionimportsetupassetup_sessionfromaiohttp_sessionimportSimpleCookieStorageasyncdefhandler_get(request):validation_error=aiohttp_flashbag.flashbag_get(request,'error')error_html=''ifvalidation_errorisnotNone:error_html='<span>{validation_error}</span>'.format(validation_error=validation_error,)body='''
        <html>
            <head><title>aiohttp_flashbag demo</title></head>
            <body>
                <form method="POST" action="/">
                    <input type="text" name="name" />
                    {error_html}
                    <input type="submit" value="Say hello">
                </form>
            </body>
        </html>
    '''body=body.format(error_html=error_html)returnweb.Response(body=body.encode('utf-8'),content_type='text/html')asyncdefhandler_post(request):post=awaitrequest.post()iflen(post['name'])==0:aiohttp_flashbag.flashbag_set(request,'error','Name is required')returnweb.HTTPSeeOther('/')body='Hello, {name}'.format(name=post['name'])returnweb.Response(body=body.encode('utf-8'),content_type='text/html')defmake_app():session_storage=SimpleCookieStorage()app=web.Application()setup_session(app,session_storage)app.middlewares.append(aiohttp_flashbag.flashbag_middleware)app.router.add_route('GET','/',handler_get,)app.router.add_route('POST','/',handler_post,)returnappweb.run_app(make_app())

首先,您必须在aiohttp.web.Application中注册aiohttp_flashbag.flashbag_middleware

您可以使用aiohttp_flashbag.flashbag_get方法从上一个请求中获取一些数据。参数:

  • 请求aiohttp.web_request.Request的实例。
  • 。要获取的“变量”的名称
  • 默认值。如果在会话闪存中不存在密钥,则应该返回默认值。

要在flashbag中设置一个“变量”,应该使用aiohttp_flashbag.flashbag_set。参数:

  • 请求aiohttp.web_request.Request的实例。
  • 。要指定的“变量”的名称。
  • 。要指定的数据。

如果需要替换flashbag中的所有“变量”,则应使用aiohttp_flashbag.flashbag_replace_all。参数:

  • 请求aiohttp.web_request.Request的实例。
  • 。使用要添加到flashbag中的值进行dict。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在java中使用鼠标在画布上绘制线而不使用Swing   java如何调试库代码中发生的NullPointerException?   java GWT在发送前在JSON中序列化POST参数   java如何在失去internet连接时停止脚本执行,并在重新连接internet时重新恢复?   尝试通过上下文菜单更新适配器时引发java Null异常   如果不更改Jersey路径,java执行器端点将无法工作   java Spring AuthenticationManager是否将有效令牌保存在内存中的某个位置?   java游戏!2.0应用程序启动时数据库覆盖。(MYSQL)   java使用spring表达式语言计算二维数组中的单元格   java为什么在这个Spring应用程序中使用JdbcTemplate BeanPropertyRowMapper类时获得这个实例化异常?   在Java中使用自定义JMXAuthenticator以编程方式从客户端远程连接到JMX的身份验证   hibernate JBoss+Java:尝试使用其他模块会话对象时出现事务未激活错误   java在JavaFX setOnAction()方法中写入OutputStream   java Spring AWS上下文凭据不工作   将@index添加到字段后,java Objectify筛选器不起作用   java我在Mac上安装了java6和java8,我使用的是哪一种?   java下面这两种语言的区别是什么?