aiohttp 和 aiohttp_jinja2 响应错误
aiohttp and aiohttp_jinja2 response error
我正在尝试使用 aiohttp_jinja2
组合 jinja2
和 aiohttp
,但事实证明这是一个错误。
我是按照documentation去做的。
没有jinja2
aiohttp
正常运行。
environment
:
env
|_index.py
|_index.tpl
index.py
:
#!/usr/bin/env python
# coding: utf-8
import asyncio
from aiohttp import web #pip install aiohttp
import aiohttp_jinja2 #pip install aiohttp_jinja2
import jinja2 #pip install jinja2
@aiohttp_jinja2.template('index.tpl')
@asyncio.coroutine
def page(req):
response = aiohttp_jinja2.render_template('index.tpl', req,{'k':'v'})
return response
@asyncio.coroutine
def init(loop):
app = web.Application(loop=loop)
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('./'))
app.router.add_route('GET', '/', page)
srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 80)
return srv
app = web.Application()
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
try: loop.run_forever()
except KeyboardInterrupt: pass
回溯错误:
Error handling request
Traceback (most recent call last):
File "/home/user/env3/lib/python3.4/site-packages/aiohttp/server.py", line 240, in start
yield from handler
File "/home/user/env3/lib/python3.4/site-packages/aiohttp/web.py", line 69, in handle_request
resp = yield from handler(request)
File "/home/user/env3/lib/python3.4/site-packages/aiohttp_jinja2/__init__.py", line 66, in wrapped
app_key=app_key, encoding=encoding)
File "/home/user/env3/lib/python3.4/site-packages/aiohttp_jinja2/__init__.py", line 38, in _render_template
text = template.render(context)
File "/home/user/env3/lib/python3.4/site-packages/jinja2/environment.py", line 964, in render
vars = dict(*args, **kwargs)
TypeError: 'Response' object is not iterable
请使用 template()
装饰器或 render_template()
函数,切勿将两者混用。
我正在尝试使用 aiohttp_jinja2
组合 jinja2
和 aiohttp
,但事实证明这是一个错误。
我是按照documentation去做的。
没有jinja2
aiohttp
正常运行。
environment
:
env
|_index.py
|_index.tpl
index.py
:
#!/usr/bin/env python
# coding: utf-8
import asyncio
from aiohttp import web #pip install aiohttp
import aiohttp_jinja2 #pip install aiohttp_jinja2
import jinja2 #pip install jinja2
@aiohttp_jinja2.template('index.tpl')
@asyncio.coroutine
def page(req):
response = aiohttp_jinja2.render_template('index.tpl', req,{'k':'v'})
return response
@asyncio.coroutine
def init(loop):
app = web.Application(loop=loop)
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('./'))
app.router.add_route('GET', '/', page)
srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 80)
return srv
app = web.Application()
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
try: loop.run_forever()
except KeyboardInterrupt: pass
回溯错误:
Error handling request
Traceback (most recent call last):
File "/home/user/env3/lib/python3.4/site-packages/aiohttp/server.py", line 240, in start
yield from handler
File "/home/user/env3/lib/python3.4/site-packages/aiohttp/web.py", line 69, in handle_request
resp = yield from handler(request)
File "/home/user/env3/lib/python3.4/site-packages/aiohttp_jinja2/__init__.py", line 66, in wrapped
app_key=app_key, encoding=encoding)
File "/home/user/env3/lib/python3.4/site-packages/aiohttp_jinja2/__init__.py", line 38, in _render_template
text = template.render(context)
File "/home/user/env3/lib/python3.4/site-packages/jinja2/environment.py", line 964, in render
vars = dict(*args, **kwargs)
TypeError: 'Response' object is not iterable
请使用 template()
装饰器或 render_template()
函数,切勿将两者混用。