简体   繁体   中英

Python Forex Calculator Not Complying

I'm trying to complete a forex money converter in FLASK but my code isn't working. It allows the user input, then once I hit submit the next page that shows Method Not Allowed The method is not allowed for the requested URL.

-Update, now it just goes to an action page, whenever I try to pull up the Index2.html it doesn't load. I'm not sure why its not allowing me to proceed.

-I'm trying to save three inputs, add them to a list, then convert them into a dollar amount -Page Refreshes to action page, this is not what is intended

Index.html file
        ><!DOCTYPE html>
<html>
<body>

<h2>HTML Forms</h2>

<form action="/index2.html", method="Post">
  <label for="C1" name= "C1"> Converting From:</label><br>
  <input type="text" id="C1" name="C1"><br>
  <label for="C2">Converting To:</label><br>
  <input type="text" id="C2_to" name="C2_to"><br>
  <label for="amt">Amount:</label><br>
  <input type="text" id="amt" name="amt"><br>
  <br>
  <input type="submit" value="Submit">
</form>

app.py file

    from flask import Flask, render_template,request, redirect, session
from forex_python.converter import CurrencyCodes, CurrencyRates



app = Flask(__name__)
app.config["secret key"]="yeah"
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False

responses_key= "responses"

@app.route("/")
def show_form():

  
    return render_template("Index.html" )

@app.route("/index2")
def show_form2():

  
    return render_template("index2.html" )

T= CurrencyRates()

@app.route("/Index2", methods=["POST"])
def save_answer():
    line2 = request.data.get("C1", "C2", int("amt"))
    responses = session[responses_key]  
    responses.append(line2)
    responses = session[responses]

    rate = T.convert(line2[0],line2[1],int(line2[2]))
    return rate,render_template("/Index2.html", rate="response")

As index2.htlm is not shared, i created a simple 1 liner index2.html to show the conversion. PFB code:

app.py:

from flask import Flask, render_template, request, redirect, session, url_for
from forex_python.converter import CurrencyCodes, CurrencyRates

app = Flask(__name__)
app.config["secret key"] = "yeah"
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False

responses_key = "responses"

@app.route('/', methods = ['GET', 'POST'])
def index():
    return render_template('Index.html')

T = CurrencyRates()

@app.route("/conversion", methods = ['GET', 'POST'])
def conversion():
    if request.method == 'POST':
        C1 = request.form.get('C1', None)
        C2_to = request.form.get('C2_to', None)
        amt = request.form.get('amt', None)
        rate = T.convert(C1, C2_to, int(amt))
        print(C1, C2_to, amt, rate)
        details = {'C1': C1, 'C2_to': C2_to, 'amt': amt, 'rate': rate}
    return render_template("Index2.html", details=details)

if __name__ == '__main__':
    app.run(host='xx.xx.xx.xx', debug=True)
    app.run(debug=True)

Index.html:

<!DOCTYPE html>
<html>
<body>

<h2>HTML Forms</h2>

<form action="{{ url_for('conversion') }}" method="post">
<label for="C1" name= "C1"> Converting From:</label><br>
<input type="text" id="C1" name="C1"><br>
<label for="C2_to" name= "C2_to">Converting To:</label><br>
<input type="text" id="C2_to" name="C2_to"><br>
<label for="amt">Amount:</label><br>
<input type="text" id="amt" name="amt"><br>
<br>
<input type="submit" value="Submit">
</form>

Index2.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <p> Conversion of {{ details.amt }} {{ details.C1 }} to {{ details.C2_to }} is: {{ details.rate }}</p>
</body>
</html>

Output:

homepage:

主页/索引页面

conversion page:

转换/index2 页面

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM