-
-
Save jie-nissel/6807eb8e22e71075e4b6494e64e6ce54 to your computer and use it in GitHub Desktop.
Checkbox WTForms Example (in Flask)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
</head> | |
<body> | |
<form method="post"> | |
{{ form.hidden_tag() }} | |
{{ form.example }} | |
<button type="submit">Submit</button> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template | |
from flask_wtf import FlaskForm | |
from wtforms import widgets, SelectMultipleField | |
SECRET_KEY = 'development' | |
app = Flask(__name__) | |
app.config.from_object(__name__) | |
class MultiCheckboxField(SelectMultipleField): | |
widget = widgets.ListWidget(prefix_label=False) | |
option_widget = widgets.CheckboxInput() | |
class SimpleForm(FlaskForm): | |
string_of_files = ['one\r\ntwo\r\nthree\r\n'] | |
list_of_files = string_of_files[0].split() | |
# create a list of value/description tuples | |
files = [(x, x) for x in list_of_files] | |
example = MultiCheckboxField('Label', choices=files) | |
@app.route('/',methods=['post','get']) | |
def hello_world(): | |
form = SimpleForm() | |
if form.validate_on_submit(): | |
print(form.example.data) | |
return render_template("success.html", data=form.example.data) | |
else: | |
print("Validation Failed") | |
print(form.errors) | |
return render_template('example.html',form=form) | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
</head> | |
<body> | |
<h1>Success</h1> | |
{{ data }} | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment