Python 字符串POST请求到烧瓶
我正在尝试使用Python 字符串POST请求到烧瓶,python,flask,Python,Flask,我正在尝试使用Flask实现一个简单的仪表板,它将: 使用“提交”按钮接受用户文本输入。将此用户输入发布到flask Flask接受此输入,对其执行一些操作,然后向另一个API发出GET请求 这个GET请求返回数据并以某种方式显示它(现在可以是console.log) 例如,使用星球大战API: 用户输入星球大战字符的名称(假定没有拼写错误) Flask读取此输入名称,并将其映射到ID号,因为接受ID号。向星球大战API发出GET请求,以获取完整的字符信息 现在,我们只需要console.log
Flask
实现一个简单的仪表板,它将:
console.log
)console.log
字符信息(例如“高度”、“质量”等)from flask import Flask, jsonify, request, render_template
import random
import json
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/form_example", methods=["GET", "POST"])
def form_example():
if request.method == "POST":
language = request.form("character_name")
starwars_dictionary = {"Luke Skywalker":"1", "C-3PO":"2", "R2-D2": "3"}
# starwars_dictionary is a dictionary with character_name:character_number key-value pairs.
# GET URL is of the form https://swapi.co/api/people/<character_number>
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)
from flask import Flask, jsonify, request, render_template
import requests
import random
import json
app = Flask(__name__)
@app.route("/index", methods=["GET", "POST"])
def index():
#character_height = "" # init a default value of empty string...seems unwieldy
if request.method == "POST":
character_name = request.form.get("character_name")
# Map user input to a numbers
starwars_dictionary = {"Luke Skywalker":"1", "C-3PO":"2", "R2-D2": "3"}
char_id = starwars_dictionary[character_name]
url = "https://swapi.co/api/people/"+char_id
response = requests.get(url)
response_dict = json.loads(response.text)
character_height = response_dict["height"]
return render_template("index.html", character_height=character_height)
return render_template("index.html")
#@app.route("/form_example", methods=["GET", "POST"])
#def form_example():
if __name__ == "__main__":
app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
<head>
<title>py-to-JS</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<h3>Sample Inputs</h3>
<ul>
<li>Luke Skywalker</li>
<li>C-3PO</li>
<li>R2-D2</li>
</ul>
<form method="POST">
Enter Name: <input type="text" name="character_name"><br>
<input type="submit" value="Submit"><br>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>py-to-JS</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<h3>Sample Inputs</h3>
<ul>
<li>Luke Skywalker</li>
<li>C-3PO</li>
<li>R2-D2</li>
</ul>
<form method="POST" action="/index">
Enter Name: <input type="text" name="character_name"><br>
<input type="submit" value="Submit"><br>
</form>
{{ character_height }}
</body>
</html>
py到JS
样本输入
- 卢克·天行者
- C-3PO
- R2-D2
输入名称:
{{字符高度}}
可能表单正在发布到/
端点,因为您没有声明表单操作
需要更像:
<form method="POST" action="/form_example">
编辑:也就是说,您可以使用单一路由功能处理此问题:
<form method="POST" action="{{ url_for('form_example') }}">
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
language = request.form("character_name")
starwars_dictionary = {"Luke Skywalker":"1", "C-3PO":"2", "R2-D2": "3"}
# Logic to query remote API ges here.
else: # Assume method is GET
return render_template("index.html")
然后使表单操作{{url\u for('index')}}