Python AIOHTTP服务器-用于加薪条件的Jinja模板

Python AIOHTTP服务器-用于加薪条件的Jinja模板,python,python-3.x,error-handling,jinja2,aiohttp,Python,Python 3.x,Error Handling,Jinja2,Aiohttp,在使用AIOHTTP Web创建站点时,我可能会定义一个路由并引发如下错误: async def index(request): if some_error_happened: raise web.HTTPException(body=b'Some error happened.') context = {'page_title': "My Site"} response = aiohttp_jinja2.render_template('index.ht

在使用AIOHTTP Web创建站点时,我可能会定义一个路由并引发如下错误:

async def index(request):
    if some_error_happened:
        raise web.HTTPException(body=b'Some error happened.')
    context = {'page_title': "My Site"}
    response = aiohttp_jinja2.render_template('index.html', request, context)
    return response
这将显示我为
body
指定的文本,但是我如何像普通路线一样使用Jinja2


我找到了该模块,但我不知道如何在aiohttp路由的上下文中使用它。

使用
aiohttp\u jinja2。呈现\u string

async def index(request):
    if some_error_happened:
        raise web.HTTPBadRequest(
            text=aiohttp_jinja2.render_template(
                'index.html', 
                request, 
                {"error": "Some error happened"},
            )
        )
    context = {'page_title': "My Site"}
    response = aiohttp_jinja2.render_template('index.html', request, context)
    return response