サンプルアプリ

例は千の言葉に匹敵します (百聞は一見にしかず)。 フォームをレンダリングするためにどのように deform を使用するかの デモンストレーションを行う Pyramid アプリケーションの例があります。

Warning

deformpyramid にはまったく依存していません; 下記の例の中では、単に Deform を使用した完全に動作する実際の アプリケーションをデモンストレーションするという目的のためだけに Pyramid を使用しています。

これが Python コードです:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os

from paste.httpserver import serve
from pyramid.config import Configurator

from colander import MappingSchema
from colander import SequenceSchema
from colander import SchemaNode
from colander import String
from colander import Boolean
from colander import Integer
from colander import Length
from colander import OneOf

from deform import ValidationFailure
from deform import Form
from deform import widget


here = os.path.dirname(os.path.abspath(__file__))

colors = (('red', 'Red'), ('green', 'Green'), ('blue', 'Blue'))

class DateSchema(MappingSchema):
    month = SchemaNode(Integer())
    year = SchemaNode(Integer())
    day = SchemaNode(Integer())

class DatesSchema(SequenceSchema):
    date = DateSchema()

class MySchema(MappingSchema):
    name = SchemaNode(String(),
                      description = 'The name of this thing')
    title = SchemaNode(String(),
                       widget = widget.TextInputWidget(size=40),
                       validator = Length(max=20),
                       description = 'A very short title')
    password = SchemaNode(String(),
                          widget = widget.CheckedPasswordWidget(),
                          validator = Length(min=5))
    is_cool = SchemaNode(Boolean(),
                         default = True)
    dates = DatesSchema()
    color = SchemaNode(String(),
                       widget = widget.RadioChoiceWidget(values=colors),
                       validator = OneOf(('red', 'blue')))

def form_view(request):
    schema = MySchema()
    myform = Form(schema, buttons=('submit',))

    if 'submit' in request.POST:
        controls = request.POST.items()
        try:
            myform.validate(controls)
        except ValidationFailure, e:
            return {'form':e.render()}
        return {'form':'OK'}

    return {'form':myform.render()}

if __name__ == '__main__':
    settings = dict(reload_templates=True)
    config = Configurator(settings=settings)
    config.add_view(form_view, renderer=os.path.join(here, 'form.pt'))
    config.add_static_view('static', 'deform:static')
    app = config.make_wsgi_app()
    serve(app)

これが Chameleon ZPT テンプレートです。 form.pt という名前で 同じディレクトリに置かれます:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
  Deform Sample Form App
</title>
<!-- Meta Tags -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- JavaScript -->
<script type="text/javascript" src="static/scripts/deform.js"></script>
<!-- CSS -->
<link rel="stylesheet" href="static/css/form.css" type="text/css" />
</head>
<body id="public">
<div id="container">
<h1>Sample Form</h1>
<span tal:replace="structure form"/>
</div>
</body>
</html>

Previous topic

ウィジェット

Next topic

Ajax フォームを使う

This Page