python数据怎么传递给html,如何将字符串从HTML传递到Python,然后再传递回HTML

Background:

I have created a very simple front end, where the user can input strings. Once entered and "check" button is clicked, I would like to pass this string as a JSON to a python string where it will do a SQL look up. Based on the SQL look the python script should pass a boolean value which should change the ? to a ✔ or a ✘.

Question:

How can I pass on a string once the "check' button is pressed as a JSON to a Python script, and pass a Boolean from Python to HTML to change the ? to a ✔ or a ✘?

Research:

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;

}

My Request

? Account Name:

? Contact Name:

? Reseller:

? Issue Date:

解决方案

This is just a sample code, which will make you understand how to pass values from client to server as well as server to client.

Asumption: 'Flask' is you current working directory

Please follow the below steps:

Install Flask

Run the below command

pip install Flask

Create a python file app.py, copy paste the below content into this file.

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()

Create a file index.html in location '/Flasak/templates' as

Welcome

Name: Submit

Create a file greet.html in location '/Flasak/templates' as

Have a good day, {{name}}

Run the python file like below

python app.py

Open a browser and hit http://localhost:5000, it will display 'welcome', along with a field to input name. Provide your name and hit Submit. Like this you can send the values to server from client.

After pressing Submit, redirection will happen, the server will receive name and send it again to client. Now you should be able to see Have a good day, along with the name provided.

Just for your reference the project's directory will be looking like below:

Flask

|

|-> templates

| |

| |-> greet.html

| |-> index.html

|

|-> app.py

Note: For better understanding, I would recommend you to go through tutorials. I hope this was helpful to you.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值