Я получаю эту ошибку jinja2.exceptions.UndefinedError: 'values' is undefined
. Простой код, который я использую со ссылкой на веб-сайте Flask http://flask.pocoo.org/docs/0.12/templating/ ниже:
@app.context_processor
def utility_processor():
def format_price(amount, currency=u'€'):
return u'{0:.2f}{1}'.format(float(amount), currency)
return dict(format_price=format_price)
@app.route("/file_t")
def file_t():
return render_template('DB.html')
@app.route("/file_ttt", methods=['POST'])
def file_ttt():
if request.method == 'POST':
bMultyTest_plot = request.values.get("bMultyTest_plot")
print(bMultyTest_plot)
return jsonify({'result': 'success', 'value': 0.44})
И код HTML-шаблона ниже:
<button type="button" onclick="visualize()">Visualize</button>
<div id='here'></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
function visualize()
{
req = $.ajax({
url : "/file_ttt",
type: "POST",
data: {"bMultyTest_plot": true}
})
req.done(function(data) {
var values = data.value;
document.getElementById("here").innerHTML ="{{format_price(values)}}";
});
}
</script>
Вместо значений переменных, если я передаю значение с плавающей запятой, функция работает. как:
document.getElementById("here").innerHTML ="{{format_price(0.33)}}";
но с переменной я получаю следующее сообщение об ошибке:
document.getElementById("demo").innerHTML ="{{format_price(values)}}";
File "<ipython-input-138-4e2a0498745e>", line 17, in format_price
return u'{0:.2f}{1}'.format(float(amount), currency)
jinja2.exceptions.UndefinedError: 'values' is undefined
10.45.65.219 - - [19/Mar/2019 09:56:12] "GET /file_t HTTP/1.1" 500 -