我正在尝试使用Flask的html 5小部件来构建RangeInput(搜索“范围”以找到它)。 我想我在HTML模板中遗漏了一些东西,因为它呈现的是:
正如你所看到的,这不是我想要的:我期待这样的事情:
所以我认为错误并不是那么棘手,但我找不到它。这是我的不同文件:
__author__ = 'laurentmeyer'
# That's my main class, don't be afraid, all is local, you cannot hack anything
from MySQL import MySQL
from flask import Flask, render_template
from Form import Form;
mysql = MySQL();
app = Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'Laurent'
app.config['MYSQL_DATABASE_PASSWORD'] = 'laurent'
app.config['MYSQL_DATABASE_DB'] = 'Houses'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
@app.route("/")
def home():
cursor = mysql.connect().cursor()
cursor.execute("SELECT * from Offers;")
data = cursor.fetchall()
form = Form(csrf_enabled=False);
if data is None:
return "No data"
else:
return render_template('DB template.html',form = form, offers = data)
if __name__ == "__main__":
app.run()
我的HTML模板:
<!DOCTYPE html>
<html>
<head>
<title>Tableau des maisons</title>
</head>
<body>
<form><p>Test: {{ form.ImportanceOfPrice }} </p></form>
<!--<table>{% for house in offers %}<tr><td>Titre: {{house[0]}}</td><td>Lien: <a href="{{house[1]}}">{{house[1]}}</a></td></tr>{% endfor %}</table>-->
</body>
</html>
最后,这是我的Form.py(非常简单):
__author__ = 'laurentmeyer'
from flask_wtf import Form
from flask_wtf.html5 import RangeInput
class Test(Form):
ImportanceOfPrice = RangeInput(0.1)
答案 0 :(得分:5)
以下代码应该有用:
from flask import Flask, render_template
from wtforms import Form
from wtforms.fields.html5 import DecimalRangeField
app = Flask(__name__)
app.config['DEBUG'] = True
class TestForm(Form):
price_importance = DecimalRangeField('Price Importance')
@app.route("/")
def home():
form = TestForm(csrf_enabled=False)
return render_template('tpl.html', form=form)
if __name__ == "__main__":
app.run()
模板tpl.html,默认情况下,必须放入文件夹'templates':
<!DOCTYPE html>
<html>
<body>
<form>
<p>{{ form.price_importance.label }}: {{ form.price_importance }}</p>
</form>
</body>
</html>
您的代码包含一些错误: