當前位置: 首頁>>代碼示例>>Python>>正文


Python aiohttp_jinja2.template方法代碼示例

本文整理匯總了Python中aiohttp_jinja2.template方法的典型用法代碼示例。如果您正苦於以下問題:Python aiohttp_jinja2.template方法的具體用法?Python aiohttp_jinja2.template怎麽用?Python aiohttp_jinja2.template使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在aiohttp_jinja2的用法示例。


在下文中一共展示了aiohttp_jinja2.template方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_url_with_query

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [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

示例2: test_url_int_param

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [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

示例3: test_static

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [as 別名]
def test_static(aiohttp_client):

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

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

    app['static_root_url'] = '/static'
    app.router.add_route('GET', '/', index)
    client = await aiohttp_client(app)

    resp = await client.get('/')
    assert 200 == resp.status
    txt = await resp.text()
    assert '/static/whatever.js' == txt 
開發者ID:aio-libs,項目名稱:aiohttp-jinja2,代碼行數:21,代碼來源:test_jinja_globals.py

示例4: test_func

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [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

示例5: test_render_class_based_view

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [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

示例6: test_convert_func_to_coroutine

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [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

示例7: test_render_not_initialized

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [as 別名]
def test_render_not_initialized():

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

    app = web.Application()

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

    req = make_mocked_request('GET', '/', app=app)
    msg = "Template engine is not initialized, " \
          "call aiohttp_jinja2.setup(..., app_key={}" \
          ") first".format(aiohttp_jinja2.APP_KEY)

    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

示例8: test_set_status

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [as 別名]
def test_set_status(aiohttp_client):

    @aiohttp_jinja2.template('tmpl.jinja2', status=201)
    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 201 == 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_render_template

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [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

示例10: test_render_template_custom_status

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [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

示例11: test_render_not_mapping

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [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

示例12: test_render_without_context

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [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

示例13: test_render_default_is_autoescaped

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [as 別名]
def test_render_default_is_autoescaped(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>'}))

    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>&lt;script&gt;alert(1)&lt;/script&gt;</html>' == txt 
開發者ID:aio-libs,項目名稱:aiohttp-jinja2,代碼行數:20,代碼來源:test_simple_renderer.py

示例14: test_render_can_disable_autoescape

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [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

示例15: test_jinja_filters

# 需要導入模塊: import aiohttp_jinja2 [as 別名]
# 或者: from aiohttp_jinja2 import template [as 別名]
def test_jinja_filters(aiohttp_client):

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

    def add_2(value):
        return value + 2

    app = web.Application()
    aiohttp_jinja2.setup(
        app,
        loader=jinja2.DictLoader({'tmpl.jinja2': "{{ 5|add_2 }}"}),
        filters={'add_2': add_2}
    )

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

    resp = await client.get('/')
    assert 200 == resp.status
    txt = await resp.text()
    assert '7' == txt 
開發者ID:aio-libs,項目名稱:aiohttp-jinja2,代碼行數:25,代碼來源:test_jinja_filters.py


注:本文中的aiohttp_jinja2.template方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。