.. Example App サンプルアプリ ============== .. An example is worth a thousand words. Here's an example `Pyramid .. `_ application demonstrating how one might use .. :mod:`deform` to render a form. 例は千の言葉に匹敵します (百聞は一見にしかず)。 フォームをレンダリングするためにどのように :mod:`deform` を使用するかの デモンストレーションを行う `Pyramid `_ アプリケーションの例があります。 .. warning:: .. :mod:`deform` is not dependent on :mod:`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. :mod:`deform` は :mod:`pyramid` にはまったく依存していません; 下記の例の中では、単に Deform を使用した完全に動作する実際の アプリケーションをデモンストレーションするという目的のためだけに Pyramid を使用しています。 .. Here's the Python code: これが Python コードです: .. code-block:: python :linenos: 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) .. Here's the Chameleon ZPT template named ``form.pt``, placed in the .. same directory: これが Chameleon ZPT テンプレートです。 ``form.pt`` という名前で 同じディレクトリに置かれます: .. code-block:: xml :linenos: Deform Sample Form App

Sample Form