【发布时间】:2021-03-14 03:33:46
【问题描述】:
我正在使用外汇转换器api构建一个小应用程序,它的功能是获取一种货币,并将一个值转换为新货币。在访问我的类“调查”时,我似乎被抓住了我试图从我的 html 表单中获取数据的所有内容。 我的程序被 self.convertFrom=request.form['convertFrom'] 捕获,python 调试器给了我“RuntimeError:Working outside of request context。”如果有人可以显示,我将不胜感激/向我解释我在这里做错了什么。
app.py
from flask_debugtoolbar import DebugToolbar
from forex_python.converter import CurrencyRates
from handleForm import Survey
app = Flask(__name__)
survey = Survey()
result=["Give me something to convert!"]
@app.route("/")
def home_page():
"""Loads home page where user can enter their first conversion"""
return render_template('index.html')
@app.route("/conversion")
def show_conversion():
"""shows the users conversion"""
return render_template('convSubmit.html', result=result)
@app.route("/conversion/new", methods=["POST"])
def add_conversion():
"""clear old conversion from list and add new"""
result=[]
result.append(survey.convertCurrency())
return redirect("/conversion")
handleForm.py
from flask import Flask, render_template, request
from forex_python.converter import CurrencyRates
c = CurrencyRates()
class Survey():
def __init__(self):
self.convertFrom=request.form['convertFrom'] <---gets caught here
self.convertTo=request.form['convertTo']
self.value=request.form['value']
def convertCurrency(self):
currencyFrom = self.convertFrom
currencyTo = self.convertTo
getValue = int(self.value)
result = c.convert(currencyFrom, currencyTo, getValue)
return result
【问题讨论】: