2017-05-25 41 views
0

應用拆分routes.py我的項目結構,如:在aiohttp

apps 
    app1 
     __init__.py 
     views.py 
    app2 
     __init__.py 
     views.py 
    __init__.py 
    main.py 
    routes.py 

如何通過應用分裂路線,把它們轉化爲自己的應用程序,並將其納入「全球」路由器像Django的包括嗎?

回答

2

你可以做這樣的事情:

在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) 

爲我工作就好:)