当前位置: 首页>>代码示例>>Python>>正文


Python jinja2.DictLoader方法代码示例

本文整理汇总了Python中jinja2.DictLoader方法的典型用法代码示例。如果您正苦于以下问题:Python jinja2.DictLoader方法的具体用法?Python jinja2.DictLoader怎么用?Python jinja2.DictLoader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在jinja2的用法示例。


在下文中一共展示了jinja2.DictLoader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_url

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_url(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def index(request):
        return {}

    async def other(request):
        return

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2':
         "{{ url('other', name='John_Doe')}}"}))

    app.router.add_route('GET', '/', index)
    app.router.add_route('GET', '/user/{name}', other, name='other')
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status
    txt = await resp.text()
    assert '/user/John_Doe' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:24,代码来源:test_jinja_globals.py

示例2: test_url_with_query

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_url_with_query(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def index(request):
        return {}

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2':
         "{{ url('index', query_={'foo': 'bar'})}}"}))

    app.router.add_get('/', index, name='index')
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status
    txt = await resp.text()
    assert '/?foo=bar' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:20,代码来源:test_jinja_globals.py

示例3: test_url_int_param

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_url_int_param(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def index(request):
        return {}

    async def other(request):
        return

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2':
         "{{ url('other', arg=1)}}"}))

    app.router.add_route('GET', '/', index)
    app.router.add_route('GET', '/uid/{arg}', other, name='other')
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status
    txt = await resp.text()
    assert '/uid/1' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:24,代码来源:test_jinja_globals.py

示例4: test_url_param_forbidden_type

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_url_param_forbidden_type(aiohttp_client):

    async def index(request):
        with pytest.raises(TypeError,
                           match=(r"argument value should be str or int, "
                                  r"got arg -> \[<class 'bool'>\] True")):
            aiohttp_jinja2.render_template('tmpl.jinja2', request, {})
        return web.Response()

    async def other(request):
        return

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2':
         "{{ url('other', arg=True)}}"}))

    app.router.add_route('GET', '/', index)
    app.router.add_route('GET', '/uid/{arg}', other, name='other')
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:25,代码来源:test_jinja_globals.py

示例5: test_helpers_disabled

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_helpers_disabled(aiohttp_client):

    async def index(request):
        with pytest.raises(jinja2.UndefinedError,
                           match="'url' is undefined"):
            aiohttp_jinja2.render_template('tmpl.jinja2', request, {})
        return web.Response()

    app = web.Application()
    aiohttp_jinja2.setup(
        app,
        default_helpers=False,
        loader=jinja2.DictLoader(
            {'tmpl.jinja2': "{{ url('index')}}"})
    )

    app.router.add_route('GET', '/', index)
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:23,代码来源:test_jinja_globals.py

示例6: test_static_var_missing

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_static_var_missing(aiohttp_client, caplog):

    async def index(request):
        with pytest.raises(RuntimeError, match='static_root_url'):
            aiohttp_jinja2.render_template('tmpl.jinja2', request, {})
        return web.Response()

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2':
         "{{ static('whatever.js') }}"}))

    app.router.add_route('GET', '/', index)
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status  # static_root_url is not set 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:19,代码来源:test_jinja_globals.py

示例7: test_func

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_func(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def func(request):
        return {'head': 'HEAD', 'text': 'text'}

    template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'
    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({
        'tmpl.jinja2': template
    }))

    app.router.add_route('*', '/', func)

    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status
    txt = await resp.text()
    assert '<html><body><h1>HEAD</h1>text</body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:22,代码来源:test_simple_renderer.py

示例8: test_render_class_based_view

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_render_class_based_view(aiohttp_client):
    class MyView(web.View):
        @aiohttp_jinja2.template('tmpl.jinja2')
        async def get(self):
            return {'head': 'HEAD', 'text': 'text'}

    template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({
        'tmpl.jinja2': template
    }))

    app.router.add_route('*', '/', MyView)

    client = await aiohttp_client(app)

    resp = await client.get('/')

    assert 200 == resp.status
    txt = await resp.text()
    assert '<html><body><h1>HEAD</h1>text</body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:24,代码来源:test_simple_renderer.py

示例9: test_convert_func_to_coroutine

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_convert_func_to_coroutine(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def func(request):
        return {'head': 'HEAD', 'text': 'text'}

    template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({
        'tmpl.jinja2': template
    }))

    app.router.add_route('*', '/', func)

    client = await aiohttp_client(app)

    resp = await client.get('/')

    txt = await resp.text()
    assert '<html><body><h1>HEAD</h1>text</body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:23,代码来源:test_simple_renderer.py

示例10: test_render_template

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_render_template(aiohttp_client):

    async def func(request):
        return aiohttp_jinja2.render_template(
            'tmpl.jinja2', request,
            {'head': 'HEAD', 'text': 'text'})

    template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({
        'tmpl.jinja2': template
    }))

    app.router.add_route('*', '/', func)

    client = await aiohttp_client(app)

    resp = await client.get('/')

    assert 200 == resp.status
    txt = await resp.text()
    assert '<html><body><h1>HEAD</h1>text</body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:25,代码来源:test_simple_renderer.py

示例11: test_render_template_custom_status

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_render_template_custom_status(aiohttp_client):

    async def func(request):
        return aiohttp_jinja2.render_template(
            'tmpl.jinja2', request,
            {'head': 'HEAD', 'text': 'text'}, status=404)

    template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>'

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({
        'tmpl.jinja2': template
    }))

    app.router.add_route('*', '/', func)

    client = await aiohttp_client(app)

    resp = await client.get('/')

    assert 404 == resp.status
    txt = await resp.text()
    assert '<html><body><h1>HEAD</h1>text</body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:25,代码来源:test_simple_renderer.py

示例12: test_template_not_found

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_template_not_found():

    async def func(request):
        return aiohttp_jinja2.render_template('template', request, {})

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({}))

    app.router.add_route('GET', '/', func)

    req = make_mocked_request('GET', '/', app=app)

    with pytest.raises(web.HTTPInternalServerError) as ctx:
        await func(req)

    t = "Template 'template' not found"
    assert t == ctx.value.text
    assert t == ctx.value.reason 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:20,代码来源:test_simple_renderer.py

示例13: test_render_not_mapping

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_render_not_mapping():

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def func(request):
        return 123

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2': 'tmpl'}))

    app.router.add_route('GET', '/', func)

    req = make_mocked_request('GET', '/', app=app)
    msg = "context should be mapping, not <class 'int'>"
    with pytest.raises(web.HTTPInternalServerError) as ctx:
        await func(req)

    assert msg == ctx.value.text 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:20,代码来源:test_simple_renderer.py

示例14: test_render_without_context

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_render_without_context(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def func(request):
        pass

    template = '<html><body><p>{{text}}</p></body></html>'

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2': template}))

    app.router.add_route('GET', '/', func)

    client = await aiohttp_client(app)
    resp = await client.get('/')

    assert 200 == resp.status
    txt = await resp.text()
    assert '<html><body><p></p></body></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:22,代码来源:test_simple_renderer.py

示例15: test_render_can_disable_autoescape

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import DictLoader [as 别名]
def test_render_can_disable_autoescape(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2')
    async def func(request):
        return {'text': '<script>alert(1)</script>'}

    app = web.Application()
    aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
        {'tmpl.jinja2': '<html>{{text}}</html>'}), autoescape=False)

    app.router.add_route('GET', '/', func)

    client = await aiohttp_client(app)
    resp = await client.get('/')

    assert 200 == resp.status
    txt = await resp.text()
    assert '<html><script>alert(1)</script></html>' == txt 
开发者ID:aio-libs,项目名称:aiohttp-jinja2,代码行数:20,代码来源:test_simple_renderer.py


注:本文中的jinja2.DictLoader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。