aiohttp如何处理onclick函数
how does aiohttp handle onclick function
我是aiohttp的新手,有以下问题,
首先,我在页面中有一个按钮,如下所示,
<body>
<button class="button" name="click_me" onclick="click_me">Click Me</button>
</body>
在python代码中,我定义函数如下,
async def click_me(request):
logger.info(f'test on click_me')
async def handler(request):
... ... default code here ... ...
但是,当我打开浏览器并点击"click_me"按钮时,调试页面显示错误,
Uncaught ReferenceError: click_me is not defined at HTMLButtonElement.onclick
如何在python脚本中定义捕捉click_me事件的函数?
不,the onclick
attribute refers to a Javascript function, that must be loaded in your page. If you want to call a Python function from that click event, you must perform an HTTP request (or whatever, there are many other ways), for instance with AJAX or fetch,到您相应服务器的 url。 例如
<body>
<button onclick="doFoo()"></button>
<script>
async function doFoo() {
const response = await fetch('http://YOURSERVER/foo')
// do whatever you want with your response
}
</script>
</body>
会调用这里定义的Pythonfoo
函数
from aiohttp import web
async def foo(request):
logger.info("button clicked")
return web.Response()
app = web.Application()
app.add_routes([web.get('/foo', foo)])
if __name__ == '__main__':
web.run_app(app)
我是aiohttp的新手,有以下问题,
首先,我在页面中有一个按钮,如下所示,
<body>
<button class="button" name="click_me" onclick="click_me">Click Me</button>
</body>
在python代码中,我定义函数如下,
async def click_me(request):
logger.info(f'test on click_me')
async def handler(request):
... ... default code here ... ...
但是,当我打开浏览器并点击"click_me"按钮时,调试页面显示错误,
Uncaught ReferenceError: click_me is not defined at HTMLButtonElement.onclick
如何在python脚本中定义捕捉click_me事件的函数?
不,the onclick
attribute refers to a Javascript function, that must be loaded in your page. If you want to call a Python function from that click event, you must perform an HTTP request (or whatever, there are many other ways), for instance with AJAX or fetch,到您相应服务器的 url。 例如
<body>
<button onclick="doFoo()"></button>
<script>
async function doFoo() {
const response = await fetch('http://YOURSERVER/foo')
// do whatever you want with your response
}
</script>
</body>
会调用这里定义的Pythonfoo
函数
from aiohttp import web
async def foo(request):
logger.info("button clicked")
return web.Response()
app = web.Application()
app.add_routes([web.get('/foo', foo)])
if __name__ == '__main__':
web.run_app(app)