Python 按aiohttp中的应用拆分routes.py
我的项目结构如下:Python 按aiohttp中的应用拆分routes.py,python,aiohttp,Python,Aiohttp,我的项目结构如下: apps app1 __init__.py views.py app2 __init__.py views.py __init__.py main.py routes.py 如何按应用程序分割路由,将它们放入自己的应用程序中,并将它们包括在“全局”路由器中,如django的include do?您可以执行以下操作: 在apps\app1\views.py中 from ai
apps
app1
__init__.py
views.py
app2
__init__.py
views.py
__init__.py
main.py
routes.py
如何按应用程序分割路由,将它们放入自己的应用程序中,并将它们包括在“全局”路由器中,如django的include do?您可以执行以下操作: 在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'}
)
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'}
)
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)
在apps\app2\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'}
)
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'}
)
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)
在routes.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'}
)
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'}
)
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)
从app1.1将导入路由视为app1\u路由
从app2.0将导入路由视为app2_路由
routes = list()
routes.append(app1_routes)
routes.extend(app2_routes)
在main.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'}
)
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'}
)
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)
对我来说很好:)你可以这样做: 在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'}
)
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'}
)
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)
在apps\app2\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'}
)
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'}
)
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)
在routes.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'}
)
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'}
)
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)
从app1.1将导入路由视为app1\u路由
从app2.0将导入路由视为app2_路由
routes = list()
routes.append(app1_routes)
routes.extend(app2_routes)
在main.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'}
)
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'}
)
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)
为我工作很好:)