[Checkins] SVN: Sandbox/ulif/grok-adminui/doc/ First part of grok
forms for grok tutorial.
Uli Fouquet
uli at gnufix.de
Sat Jun 30 11:17:58 EDT 2007
Log message for revision 77270:
First part of grok forms for grok tutorial.
Changed:
A Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/
A Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/buildout.cfg
A Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/setup.py
A Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/
A Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/
A Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/__init__.py
A Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/app.py
A Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/app_templates/
A Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/app_templates/index.pt
A Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/configure.zcml
U Sandbox/ulif/grok-adminui/doc/tutorial.txt
-=-
Added: Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/buildout.cfg
===================================================================
--- Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/buildout.cfg (rev 0)
+++ Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/buildout.cfg 2007-06-30 15:17:57 UTC (rev 77270)
@@ -0,0 +1,3 @@
+[buildout]
+develop = .
+extends = ../buildout_tut.cfg
\ No newline at end of file
Added: Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/setup.py
===================================================================
--- Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/setup.py (rev 0)
+++ Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/setup.py 2007-06-30 15:17:57 UTC (rev 77270)
@@ -0,0 +1,28 @@
+from setuptools import setup, find_packages
+
+version = 0.0
+
+setup(name='Sample',
+ version=version,
+ description="",
+ long_description="""\
+""",
+ # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers
+ classifiers=[],
+ keywords="",
+ author="",
+ author_email="",
+ url="",
+ license="",
+ package_dir={'': 'src'},
+ packages=find_packages('src'),
+ include_package_data=True,
+ zip_safe=False,
+ install_requires=['setuptools',
+ 'grok',
+ # -*- Extra requirements: -*-
+ ],
+ entry_points="""
+ # -*- Entry points: -*-
+ """,
+ )
Added: Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/__init__.py
===================================================================
--- Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/__init__.py (rev 0)
+++ Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/__init__.py 2007-06-30 15:17:57 UTC (rev 77270)
@@ -0,0 +1 @@
+#
Added: Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/app.py
===================================================================
--- Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/app.py (rev 0)
+++ Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/app.py 2007-06-30 15:17:57 UTC (rev 77270)
@@ -0,0 +1,12 @@
+import grok
+from zope.schema import TextLine
+
+class Sample(grok.Application, grok.Container):
+ text = 'default text'
+
+class Index(grok.View):
+ pass
+
+class Edit(grok.EditForm):
+ form_fields = grok.Fields(
+ text=TextLine(title=u'The text to store:'))
Added: Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/app_templates/index.pt
===================================================================
--- Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/app_templates/index.pt (rev 0)
+++ Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/app_templates/index.pt 2007-06-30 15:17:57 UTC (rev 77270)
@@ -0,0 +1,5 @@
+<html>
+<body>
+<p>The text: <span tal:replace="python:context.text">text</span></p>
+</body>
+</html>
Added: Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/configure.zcml
===================================================================
--- Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/configure.zcml (rev 0)
+++ Sandbox/ulif/grok-adminui/doc/groktut/building_forms_with_formlib/src/sample/configure.zcml 2007-06-30 15:17:57 UTC (rev 77270)
@@ -0,0 +1 @@
+<grok package="." xmlns="http://namespaces.zope.org/grok" />
Modified: Sandbox/ulif/grok-adminui/doc/tutorial.txt
===================================================================
--- Sandbox/ulif/grok-adminui/doc/tutorial.txt 2007-06-30 15:03:53 UTC (rev 77269)
+++ Sandbox/ulif/grok-adminui/doc/tutorial.txt 2007-06-30 15:17:57 UTC (rev 77270)
@@ -1404,3 +1404,95 @@
instances of ``Entry``, but also other containers. If you made those
modifications, you would be on your way to building your own content
management system with Grok.
+
+
+Building forms with formlib
+===========================
+
+Up to now, we created forms using basically plain HTML and
+implementing every apect of 'logic' using Python code. This is fine
+for some cases but often you would like to have a more automatic way
+to create forms. There is still a lot of repetition in the code. For
+instance we tell several times what values we would like to
+edit/display and what kind of data we would like to have stored
+therein. Furthermore, when the model we use changes slightly, we have
+to maintain a lot of associated logic. Every piece of validation code
+has to be reviewed and eventually rewritten. Last not least you cannot
+easily reuse your existing validation for other models, where you have
+a similar kind of data.
+
+It would be nice, if we could just tell what kind of data we
+have and get appropriate forms without too much hassle. This is
+possible with grok and it is easy.
+
+.. sidebar:: `zope.formlib`: the name of the magic behind forms.
+
+
+ Grok does nearly nothing to provide the magic of forms as discussed
+ here. Instead it makes use of a package quite popular in Zope 3, the
+ `zope.formlib` package.
+
+ You can learn more about ``formlib`` in the documentation, which
+ comes with the package itself:
+
+ http://svn.zope.org/zope.formlib/trunk/src/zope/formlib/form.txt
+
+
+In Grok the creation of forms is supported by special form objects:
+``grok.EditForm``, ``grok.DisplayForm`` and ``grok.AddForm``. These
+are in fact ``grok.Views`` as well, but offer in addition a special
+machinery, which helps you to generate forms (in extreme) without even
+a single line of HTML code.
+
+
+Let's create a simple Form using ``grok.EditForm``. For that purpose
+we start with the application as coded above in section `Storing
+data`_ but we turn the ``Edit`` class in ``app.py``, which is a
+``grok.View`` into a ``grok.EditForm`` and import a ``schema`` called
+``TextLine`` at the beginning:
+
+.. include:: groktut/building_forms_with_formlib/src/sample/app.py
+ :literal:
+
+When you view the edit form of this application going to
+
+ http://localhost:8080/test/edit
+
+you will see an input field labeled `Text to store:` and a button
+entitled `Store`. This is nothing new. Ugh? What happened? The reason
+is, that there is still a template ``edit.pt`` associated with our new
+form, which renders the form the good old fashioned way and makes no
+use at all from our new class. You *can* specify page templates to
+render the output of forms, just as you can do with ordinary
+``grok.View``, but you don't have to. Now remove the page template
+``edit.pt``.
+
+ $ rm app_templates/edit.pt
+
+and restart your Zope instance.
+
+When you look again at
+
+ http://localhost:8080/test/edit
+
+you should be able to notice some changes: There will be an asterisk
+(`*`) in front of the field label, indicating that this field is a
+required one. The text of the submit button should have turned to
+`Apply`. This is the look of a form that was completely
+autogenerated. Note, that we didn't write one line of HTML to get a
+complete form. And it works.
+
+If you leave the text in the field untouched and click the `Apply`
+button, you will get a message on top, telling you that there were no
+changes.
+
+If you change the text and apply a new value, the form will display a
+timestamp instead, telling that the data was
+updated. Congratulations. You created your first *real* form with
+Grok. Looking back to the first forms created above, they appear
+nowmere as some conglomeration of Python code and HTML wich just
+accidently happens to be a HTML form.
+
+Defining field types using schemas
+----------------------------------
+
More information about the Checkins
mailing list