在aiohttp
问题描述:
应用拆分routes.py我的项目结构,如:在aiohttp
apps
app1
__init__.py
views.py
app2
__init__.py
views.py
__init__.py
main.py
routes.py
如何通过应用分裂路线,把它们转化为自己的应用程序,并将其纳入“全球”路由器像Django的包括吗?
答
你可以做这样的事情:
在APPS \ APP1 \ views.py
from aiohttp import web
async def route_path_def(request):
return web.Response(body=b'Some response')
routes = (
{'GET', '/route_path', route_path_def, 'route_path_name'}
)
在APPS \ APP 2 \ views.py
from aiohttp import web
async def another_route_path_def(request):
return web.Response(body=b'Some response')
routes = (
{'GET', '/another_route_path', another_route_path_def, 'another_route_path_name'}
)
在routes.py
从app1.views的进口途径为app1_routes从app2.views的进口途径 作为app2_routes
routes = list()
routes.append(app1_routes)
routes.extend(app2_routes)
在main.py
from .routes import routes
from aiohttp import web
app = web.Application()
for method, path, func_name, page_name in routes:
app.router.add_route(method, path, func_name, name=page_name)
web.run_app(app, port=5000)
为我工作就好:)