lhy
lhy

Reputation: 11

Why is my PyScript code not working when trying to get input values from a form and send them to the backend?

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>

What I've tried:

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

Answers (0)

Related Questions