Example AppΒΆ
An example is worth a thousand words. Here is an example Pyramid application demonstrating how one might use
deform
to render a form. For it to work, you will need to have
deform
, pyramid
, and pyramid_chameleon
installed in your
Python environment.
Note
deform
is not dependent on pyramid
at all. We use
Pyramid in the examples below only to facilitate demonstration of
an actual end-to-end working application that uses Deform.
Here is the Python code:
1import os
2
3from wsgiref.simple_server import make_server
4from pyramid.config import Configurator
5
6from colander import (
7 Boolean,
8 Integer,
9 Length,
10 MappingSchema,
11 OneOf,
12 SchemaNode,
13 SequenceSchema,
14 String
15)
16
17from deform import (
18 Form,
19 ValidationFailure,
20 widget
21)
22
23
24here = os.path.dirname(os.path.abspath(__file__))
25
26colors = (('red', 'Red'), ('green', 'Green'), ('blue', 'Blue'))
27
28class DateSchema(MappingSchema):
29 month = SchemaNode(Integer())
30 year = SchemaNode(Integer())
31 day = SchemaNode(Integer())
32
33class DatesSchema(SequenceSchema):
34 date = DateSchema()
35
36class MySchema(MappingSchema):
37 name = SchemaNode(String(),
38 description = 'The name of this thing')
39 title = SchemaNode(String(),
40 widget = widget.TextInputWidget(size=40),
41 validator = Length(max=20),
42 description = 'A very short title')
43 password = SchemaNode(String(),
44 widget = widget.CheckedPasswordWidget(),
45 validator = Length(min=5))
46 is_cool = SchemaNode(Boolean(),
47 default = True)
48 dates = DatesSchema()
49 color = SchemaNode(String(),
50 widget = widget.RadioChoiceWidget(values=colors),
51 validator = OneOf(('red', 'blue')))
52
53def form_view(request):
54 schema = MySchema()
55 myform = Form(schema, buttons=('submit',))
56 template_values = {}
57 template_values.update(myform.get_widget_resources())
58
59 if 'submit' in request.POST:
60 controls = request.POST.items()
61 try:
62 myform.validate(controls)
63 except ValidationFailure as e:
64 template_values['form'] = e.render()
65 else:
66 template_values['form'] = 'OK'
67 return template_values
68
69 template_values['form'] = myform.render()
70 return template_values
71
72if __name__ == '__main__':
73 settings = dict(reload_templates=True)
74 config = Configurator(settings=settings)
75 config.include('pyramid_chameleon')
76 config.add_view(form_view, renderer=os.path.join(here, 'form.pt'))
77 config.add_static_view('static', 'deform:static')
78 app = config.make_wsgi_app()
79 server = make_server('0.0.0.0', 8080, app)
80 server.serve_forever()
Here is the Chameleon ZPT template named form.pt
, placed in the same directory:
1<!doctype html>
2<html>
3 <head>
4 <meta charset="utf-8">
5 <title>Deform Sample Form App</title>
6 <meta name="viewport" content="width=device-width, initial-scale=1">
7
8 <!-- JavaScript -->
9 <script src="static/scripts/jquery-2.0.3.min.js"></script>
10 <script src="static/scripts/bootstrap.min.js"></script>
11 <tal:loop tal:repeat="js_resource js">
12 <script src="${request.static_path(js_resource)}"></script>
13 </tal:loop>
14
15 <!-- CSS -->
16 <link rel="stylesheet" href="static/css/bootstrap.min.css"
17 type="text/css">
18 <link rel="stylesheet" href="static/css/form.css" type="text/css">
19 <tal:loop tal:repeat="css_resource css">
20 <link rel="stylesheet" href="${request.static_path(css_resource)}"
21 type="text/css">
22 </tal:loop>
23
24 </head>
25 <body>
26 <div class="container">
27 <div class="row">
28 <div class="col-md-12">
29 <h1>Sample Form</h1>
30 <span tal:replace="structure form"/>
31 </div>
32 </div>
33 </div>
34 </body>
35</html>