【发布时间】:2018-04-14 07:47:59
【问题描述】:
我收到如下错误消息:TypeError: unsupported format string passing to method.__format __.
我使用的是 Linux 环境,这段代码应该计算利率。有单独的文件用于运行脚本和函数。但是,一旦在页面中输入数据,我就会在页面上看到一条错误消息,指出以下代码之前的格式不正确:
return render_template('interest_form.html', totale="{0:.2f}".format(totale))
以下是完整代码:
from server import app, valid_time
from flask import request, render_template
from Calculator import Calculator
@app.route("/", methods=["POST", "GET"])
def interest_total():
if request.method == 'POST':
initial=float(request.form["initial"])
rate=float(request.form["rate"])
time=float(request.form["time"])
data=Calculator(initial,rate)
data.total_interest(time)
totale=data.total_interest
return render_template('interest_form.html', totale="{0:.2f}".format(totale))
return render_template('interest_form.html', calc_total=True)
@app.route('/time', methods=["POST", "GET"])
def time_interest():
if request.method == 'POST':
initial=float(request.form["initial"])
rate=float(request.form["rate"])
total=float(request.form["total"])
dataa=Calculator(initial,rate)
dataa.time_required(total)
time=dataa.time_required
return render_template('interest_form.html', time="{0:.2f}".format(time))
return render_template('interest_form.html', calc_time=True)
@app.route('/credits', methods=['GET'])
def credits():
return render_template('credits.html')
这是计算器部分的代码:
class Calculator:
def __init__(self, initial, rate):
"""
Pass in the initial amount invested
and the rate as a percentage (ie 50 for 50%)
"""
self._initial = initial
self._rate = rate/100
def total_interest(self, time):
"""
Returns the total amoun of interest after a given length of time
"""
return self._initial * self._rate * time
def time_required(self, total):
"""
Returns the length of time required to achieve a given total
"""
return total/(self._initial * self._rate)
我正在尝试使用 html 表单来发送输入:
{% extends 'base.html' %}
{% block content %}
<html>
<body>
<div>
<form action="" method='POST'>
<div style="margin: 10px 0px">
<label>Amount Invested ($): </label><br/>
<input name="initial" input type="number" step="0.01" placeholder="Amount Invested"/>
</div>
<div style="margin: 10px 0px">
<label>Interest Rate (%): </label><br/>
<input name="rate" input type="number" step="0.01" placeholder="Amount Invested"/>
</div>
<div style="margin: 10px 0px">
<label>Time Investment (Years): </label><br/>
<input name="time" input type="number" step="0.01" placeholder="Amount Invested" {%if calc_time %}disabled{% endif %}/>
</div>
<div style="margin: 10px 0px">
<label>Total Interest ($): </label><br/>
<input name="total" input type="number" step="0.01" placeholder="Amount Invested" {%if calc_total %}disabled{% endif %}/>
</div>
<div style="margin: 10px 0px">
<button type="submit">Submit</button>
</div>
{% if totale %}<h4>Total amount is</h4>
<textarea name="Amount" rows="5" cols="20"> {{totale}}</textarea>
{% endif %}
</form>
{% if time %}<h4>Total amount is</h4>
<textarea name="Amount" rows="5" cols="20"> {{time}}</textarea>
{% endif %}
</form>
</div>
<div>
<a href="{{url_for('time_interest')}}">Time_Form</a>
<a href="{{url_for('interest_total')}}">Total Form</a>
<br/><a href="{{url_for('credits')}}">Credits Page</a>
</div>
</body>
</html>
{% endblock %}
我使用了一个不同的模板代码,它具有类似的功能和返回格式,运行它时似乎没有问题。然而,这似乎显示了上述错误消息。
【问题讨论】:
标签: python