互联网服务供应商

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第三方库


热门话题
Hibernate:jpamodelgen生成java而不是类   java如何在第二次活动结束后显示广告?   javascript如何在linux或windows上将java导出到jar   java One ArrayList添加了2个不同的选项   AmazonWeb服务如何在java中使用AWS Textract检索pdf中存在的表   java为什么RecycleView中的水平项在单击时不起作用?   java计算如果存在映射   java在捕获的组上应用正则表达式   如何使用Java在MySQL的同一个表中插入来自不同类的值   java字符串中最常见的字母(大写和小写)   Spring SessionBean实例正在由java中的多个用户共享   使用Spring Boot将@WebInitParam中的值外部化   java一创建线程就开始执行(多线程)   java是安卓所需的系统权限。意图行动给你打电话?