|
1
|
|
|
from flask import Flask, render_template, flash |
|
2
|
|
|
from flask_material import Material |
|
3
|
|
|
from flask_appconfig import AppConfig |
|
4
|
|
|
from flask_wtf import Form, RecaptchaField |
|
5
|
|
|
from flask_wtf.file import FileField |
|
6
|
|
|
from wtforms import TextField, HiddenField, ValidationError, RadioField,\ |
|
7
|
|
|
BooleanField, SubmitField, IntegerField, FormField, validators |
|
8
|
|
|
from wtforms.validators import Required |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
# straight from the wtforms docs: |
|
12
|
|
|
class TelephoneForm(Form): |
|
13
|
|
|
country_code = IntegerField('Country Code', [validators.required()]) |
|
14
|
|
|
area_code = IntegerField('Area Code/Exchange', [validators.required()]) |
|
15
|
|
|
number = TextField('Number') |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
class ExampleForm(Form): |
|
19
|
|
|
field1 = TextField('First Field', description='This is field one.') |
|
20
|
|
|
field2 = TextField('Second Field', description='This is field two.', |
|
21
|
|
|
validators=[Required()]) |
|
22
|
|
|
hidden_field = HiddenField('You cannot see this', description='Nope') |
|
23
|
|
|
recaptcha = RecaptchaField('A sample recaptcha field') |
|
24
|
|
|
radio_field = RadioField('This is a radio field', choices=[ |
|
25
|
|
|
('head_radio', 'Head radio'), |
|
26
|
|
|
('radio_76fm', "Radio '76 FM"), |
|
27
|
|
|
('lips_106', 'Lips 106'), |
|
28
|
|
|
('wctr', 'WCTR'), |
|
29
|
|
|
]) |
|
30
|
|
|
checkbox_field = BooleanField('This is a checkbox', |
|
31
|
|
|
description='Checkboxes can be tricky.') |
|
32
|
|
|
|
|
33
|
|
|
# subforms |
|
34
|
|
|
mobile_phone = FormField(TelephoneForm) |
|
35
|
|
|
|
|
36
|
|
|
# you can change the label as well |
|
37
|
|
|
office_phone = FormField(TelephoneForm, label='Your office phone') |
|
38
|
|
|
|
|
39
|
|
|
ff = FileField('Sample upload') |
|
40
|
|
|
|
|
41
|
|
|
submit_button = SubmitField('Submit Form') |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def validate_hidden_field(form, field): |
|
45
|
|
|
raise ValidationError('Always wrong') |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def create_app(configfile=None): |
|
49
|
|
|
app = Flask(__name__) |
|
50
|
|
|
AppConfig(app, configfile) # Flask-Appconfig is not necessary, but |
|
51
|
|
|
# highly recommend =) |
|
52
|
|
|
# https://github.com/mbr/flask-appconfig |
|
53
|
|
|
Material(app) |
|
54
|
|
|
|
|
55
|
|
|
# in a real app, these should be configured through Flask-Appconfig |
|
56
|
|
|
app.config['SECRET_KEY'] = 'devkey' |
|
57
|
|
|
app.config['RECAPTCHA_PUBLIC_KEY'] = \ |
|
58
|
|
|
'6Lfol9cSAAAAADAkodaYl9wvQCwBMr3qGR_PPHcw' |
|
59
|
|
|
|
|
60
|
|
|
@app.route('/', methods=('GET', 'POST')) |
|
61
|
|
|
def index(): |
|
62
|
|
|
form = ExampleForm() |
|
63
|
|
|
form.validate_on_submit() # to get error messages to the browser |
|
64
|
|
|
flash('critical message', 'critical') |
|
65
|
|
|
flash('error message', 'error') |
|
66
|
|
|
flash('warning message', 'warning') |
|
67
|
|
|
flash('info message', 'info') |
|
68
|
|
|
flash('debug message', 'debug') |
|
69
|
|
|
flash('different message', 'different') |
|
70
|
|
|
flash('uncategorized message') |
|
71
|
|
|
return render_template('index.html', form=form) |
|
72
|
|
|
|
|
73
|
|
|
return app |
|
74
|
|
|
|
|
75
|
|
|
if __name__ == '__main__': |
|
76
|
|
|
create_app().run(debug=True) |
|
77
|
|
|
|