你可以做這樣的事情:
在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)
爲我工作就好:)