背景:
我创建了一个非常简单的前端,用户可以在其中输入字符串。输入并单击“检查”按钮后,我想将此字符串作为 JSON 传递到 python 字符串,在其中执行 SQL 查找。根据 SQL 的外观,python 脚本应该传递一个布尔值,该值应该更改 ?到 ✔ 或 ✘。
问题:
按下“检查”按钮后,如何将字符串作为 JSON 传递到 Python 脚本,并将布尔值从 Python 传递到 HTML 以将 ? 更改为 ✔ 或 ✘?
研究:
- 如何将 jquery 变量传递给 Python 脚本?
- 使用 FLASK 和 jquery 发送和接收数据
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box;}
input[type=text], select, textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
margin-top: 6px;
margin-bottom: 16px;
resize: vertical;
}
input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #45a049;
}
.container {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
h3 {text-align: center;}
.center {
display: flex;
justify-content: center;
align-items: center;
}
</style>
</style>
</head>
<body>
<h3>My Request</h3>
<div class="container">
<form action="/action_page.php">
<label for="account_name">? Account Name:</label>
<input type="text" id="fname" name="firstname" placeholder="Account Name..">
<input type="submit" value="Check Account"><br><br>
<label for="contact_name">? Contact Name:</label>
<input type="text" id="lname" name="lastname" placeholder="Contact Name..">
<input type="submit" value="Check Contact"><br><br>
<label for="reseller">? Reseller:</label>
<input type="text" id="lname" name="lastname" placeholder="Reseller..">
<input type="submit" value="Check Reseller"><br><br>
<label for="issue_date">? Issue Date:</label><br>
<input type="date" id="start" name="trip-start" value="" min="2018-01-01" max="2100-12-31">
<br>
<div class="center">
<input type="submit" value="VERIFY ALL">
</div>
</form>
</div>
</body>
</html>
这只是一个示例代码,它将使您了解如何将值从客户端传递到服务器以及服务器到客户端。
假设:“Flask”是您当前的工作目录
请按照以下步骤操作:
- 安装烧瓶
运行以下命令
pip install Flask
- 创建一个Python文件
app.py
,将以下内容复制粘贴到此文件中。
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/greet', methods=['POST'])
def greet():
name = request.form['name']
return render_template('greet.html', name=name)
if __name__ == '__main__':
app.run()
- 创建文件
index.html
在位置 '/Flasak/templates' 中
<h1>Welcome</h1>
<form action="http://localhost:5000/greet" method="POST">
Name: <input type="text" name="name"> <button type="submit">Submit</button>
</form>
- 创建文件
greet.html
在位置 '/Flasak/templates' 中
<h2>Have a good day, {{name}}</h2>
- 运行 python 文件如下
python app.py
打开浏览器并点击http://localhost:5000
,它将显示“欢迎”,以及一个用于输入名称的字段。提供您的姓名并点击Submit
。像这样,您可以将值从客户端发送到服务器。
按下后Submit
,会发生重定向,服务器会收到name
并再次发送给客户。现在你应该能够看到Have a good day,
以及提供的名称。
仅供参考,项目目录如下所示:
Flask
|
|-> templates
| |
| |-> greet.html
| |-> index.html
|
|-> app.py
Note:为了更好地理解,我建议您阅读教程。我希望这对您有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)