从flask请求方法获取用户数据

问题描述 投票:0回答:0

我对 Flask 还是很陌生,更不用说 Python 了,我希望能够从 index.py 的下拉列表中获取数据并在我的 main.html 代码中为其分配一个变量。但是,当我运行程序时,我的 TOcurrency 值是空的。任何帮助表示赞赏。

main.html

<html lang="en">
<head>
    <title>Exchange list</title>
</head>
<body>
    <div class = 'Container'> 
        <form acion = '/' method = "POST" id = 'myform'>
            <div class = "amount">
                <input type="number" name = 'amount' value='0'>
            </div>
            <div class = 'drop-list'>
                <div class = 'from' name = 'from_code'>From
                    <select>
                        {% for i in countries %}
                        <option value = {i}>{{i}}</option>
                        {% endfor %}
                    </select>
                </div>
                <div type = 'to' name = 'to_code'>To
                    <select>
                        {% for i in countries %}
                        <option value = {i}>{{i}}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>
            <div><input type = 'number' name = 'result'></div>
            <button>Convert Currency</div>
        </form> 
    </div>
</body>
</html>

index.py代码

from flask import Flask, render_template, request
import requests
import json
from api import *

app = Flask(__name__)

@app.route("/", methods = ['GET', 'POST'])
def home():
    if request.method == 'POST':
        amount = request.form['amount']
        TOcurrency = request.form.getlist('to_code')
        # FROMcurency = request.form['from_code']
        print(amount,TOcurrency)
    
    with open('exchange.json') as json_file:
        data = json.load(json_file)
        countryMap = data['conversion_rates']
        countries = countryMap.keys()
        print(countries)
        
    return render_template('main.html', countries = countries)

if __name__ == "__main__":
    app.run(debug=True)
python api flask post get
© www.soinside.com 2019 - 2024. All rights reserved.