Reputation: 11
I am trying to use PyScript to handle a form submission, get input values, and send them to a backend using a custom Python function. However, when I try to submit the form, it doesn't seem to trigger the backend request or output the values correctly.
Here is the relevant part of my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PyScript</title>
<link rel="stylesheet" href="https://pyscript.net/releases/2024.11.1/core.css">
<script type="module" src="https://pyscript.net/releases/2024.11.1/core.js"></script>
<script src="https://cdn.jsdelivr.net/pyodide/dev/full/pyodide.js"></script>
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<h2>PyScript</h2>
<!-- 用于显示结果 -->
<div id="result"></div>
<form onsubmit="return false">
Update ID: <input type="text" id="ID"><br>
Update Keywords: <input type="text" id="keyword"><br>
<input pys-onClick="handle_submit" type="submit" id="submitBtn" value="submit">
</form>
<py-script>
from pyscript import fetch, document
async def handle_submit(event):
ID = document.querySelector('#ID').value
keyword = document.querySelector('#keyword').value
print(f"ID: {ID}")
print(f"Keyword: {keyword}")
# Attempt to send the data to the backend
await client.Update('add', (ID, keyword))
</py-script>
</body>
</html>
The form submission is not triggering the handle_submit
function, and I am not seeing the expected log outputs for the ID
and keyword
.
I expect the ID
and keyword
input values to be printed and then sent to the backend, but nothing happens when I click "submit."
What I've tried:
Ensured that the onsubmit
attribute is return false
, so it doesn't reload the page.
Checked if the handle_submit
function is defined correctly in the <py-script>
section.
Ensured that pys-onClick="handle_submit"
is set correctly in the form button.
Error Messages:
I don't see any specific errors in the console, but the expected behavior does not occur.
Any help in understanding why this isn't working or how to debug it would be greatly appreciated!
Upvotes: 0
Views: 67