本文整理汇总了Python中aiohttp_jinja2.render_template方法的典型用法代码示例。如果您正苦于以下问题:Python aiohttp_jinja2.render_template方法的具体用法?Python aiohttp_jinja2.render_template怎么用?Python aiohttp_jinja2.render_template使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp_jinja2
的用法示例。
在下文中一共展示了aiohttp_jinja2.render_template方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_url_param_forbidden_type
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [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
示例2: test_helpers_disabled
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [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
示例3: test_static_var_missing
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [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
示例4: test_render_not_initialized
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_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
示例5: test_render_template
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_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
示例6: test_template_not_found
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [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
示例7: normal
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [as 别名]
def normal(self, request):
"""适配浏览器中进行游戏的页面,该页面会检查会话中是否有api_token、api_starttime和world_ip三个参数,缺少其中任意一个都不能进行
游戏,跳转回登录页面。
:param request: aiohttp.web.Request
:return: aiohttp.web.Response or aiohttp.web.HTTPFound
"""
session = yield from get_session(request)
token = session.get('api_token', None)
starttime = session.get('api_starttime', None)
world_ip = session.get('world_ip', None)
if token and starttime and world_ip:
context = {'scheme': request.scheme,
'host': request.host,
'token': token,
'starttime': starttime}
return aiohttp_jinja2.render_template('normal.html', request, context)
else:
self.clear_session(session)
return aiohttp.web.HTTPFound('/')
示例8: kcv
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [as 别名]
def kcv(self, request):
"""适配KanColleViewer或者74EO中进行游戏的页面,提供一个iframe,在iframe中载入游戏FLASH。该页面会检查会话中是否有api_token、
api_starttime和world_ip三个参数,缺少其中任意一个都不能进行游戏,跳转回登录页面。
:param request: aiohttp.web.Request
:return: aiohttp.web.Response or aiohttp.web.HTTPFound
"""
session = yield from get_session(request)
token = session.get('api_token', None)
starttime = session.get('api_starttime', None)
world_ip = session.get('world_ip', None)
if token and starttime and world_ip:
return aiohttp_jinja2.render_template('kcv.html', request, context={})
else:
self.clear_session(session)
return aiohttp.web.HTTPFound('/')
示例9: flash
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [as 别名]
def flash(self, request):
"""适配KanColleViewer或者74EO中进行游戏的页面,展示,该页面会检查会话中是否有api_token、api_starttime和world_ip三个参数,
缺少其中任意一个都不能进行游戏,跳转回登录页面。
:param request: aiohttp.web.Request
:return: aiohttp.web.Response or aiohttp.web.HTTPFound
"""
session = yield from get_session(request)
token = session.get('api_token', None)
starttime = session.get('api_starttime', None)
world_ip = session.get('world_ip', None)
if token and starttime and world_ip:
context = {'scheme': request.scheme,
'host': request.host,
'token': token,
'starttime': starttime}
return aiohttp_jinja2.render_template('flash.html', request, context)
else:
self.clear_session(session)
return aiohttp.web.HTTPFound('/')
示例10: poi
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [as 别名]
def poi(self, request):
"""适配poi中进行游戏的页面,显示FLASH。该页面会检查会话中是否有api_token、api_starttime和world_ip三个参数,缺少其中任意一个
都不能进行游戏,跳转回登录页面。
:param request: aiohttp.web.Request
:return: aiohttp.web.Response or aiohttp.web.HTTPFound
"""
session = yield from get_session(request)
token = session.get('api_token', None)
starttime = session.get('api_starttime', None)
world_ip = session.get('world_ip', None)
if token and starttime and world_ip:
context = {'scheme': request.scheme,
'host': request.host,
'token': token,
'starttime': starttime}
return aiohttp_jinja2.render_template('poi.html', request, context)
else:
self.clear_session(session)
return aiohttp.web.HTTPFound('/')
示例11: reset_password_allowed
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [as 别名]
def reset_password_allowed(request, confirmation):
db = cfg.STORAGE
form = await forms.get('ResetPassword').init(request)
user = await db.get_user({'id': confirmation['user_id']})
assert user
while request.method == 'POST' and form.validate():
await db.update_user(
user, {'password': encrypt_password(form.password.data)})
await db.delete_confirmation(confirmation)
await authorize_user(request, user)
flash.success(request, cfg.MSG_PASSWORD_CHANGED)
flash.success(request, cfg.MSG_LOGGED_IN)
return redirect(cfg.LOGIN_REDIRECT)
return render_template(themed('reset_password_allowed.html'), request, {
'auth': {
'url_for': url_for,
'cfg': cfg,
'form': form,
}
})
示例12: change_password
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [as 别名]
def change_password(request):
db = cfg.STORAGE
user = request[cfg.REQUEST_USER_KEY]
form = await forms.get('ChangePassword').init(request)
while request.method == 'POST' and form.validate():
if not check_password(form.cur_password.data, user['password']):
form.cur_password.errors.append(cfg.MSG_WRONG_PASSWORD)
break
password = encrypt_password(form.new_password.data)
await db.update_user(user, {'password': password})
flash.success(request, cfg.MSG_PASSWORD_CHANGED)
return redirect(request.path)
return render_template(themed('change_password.html'), request, {
'auth': {
'cfg': cfg,
'form': form,
'url_for': url_for,
}
})
示例13: handler
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [as 别名]
def handler(request):
await asyncio.sleep(0.01)
session = request.app['session']
resp = await session.get(service_b_api)
data_b = await resp.json()
resp = await session.get(service_e_api)
data_e = await resp.json()
tree = {
'name': 'service_a',
'host': host,
'port': port,
'children': [data_b, data_e],
}
ctx = {'zipkin': zipkin_ui_address, 'service': tree}
return aiohttp_jinja2.render_template('index.html', request, ctx)
示例14: landing
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [as 别名]
def landing(self, request):
access = await self.auth_svc.get_permissions(request)
if not access:
return render_template('login.html', request, dict())
plugins = await self.data_svc.locate('plugins', {'access': tuple(access), **dict(enabled=True)})
data = dict(plugins=[p.display for p in plugins], errors=self.app_svc.errors + self._request_errors(request), version=self.app_svc.version)
return render_template('%s.html' % access[0].name, request, data)
示例15: index_page
# 需要导入模块: import aiohttp_jinja2 [as 别名]
# 或者: from aiohttp_jinja2 import render_template [as 别名]
def index_page(self, request):
t = self._template
context = {'name': self._name}
return render_template(t, request, context, app_key=TEMPLATE_APP_KEY)