Ralph Bean avatar Ralph Bean committed 3d1def6

Basic form

Comments (0)

Files changed (5)

myapp/myapp/__init__.py

     config.add_view('myapp.views.view_model',
                     context='myapp.models.MyModel',
                     renderer="templates/model.pt")
+
+    config.add_view('myapp.views.view_widget',
+                    context='myapp.widgets.MovieForm',
+                    renderer="templates/widget.pt")
+
     return config.make_wsgi_app()
 
 

myapp/myapp/models.py

+import myapp.widgets
 import transaction
 
 from sqlalchemy.orm import scoped_session
     __parent__ = None
 
     def __getitem__(self, key):
+        if key == 'movie':
+            w = myapp.widgets.MovieForm.req()
+            w.__parent__ = self
+            w.__name__ = key
+            return w
+
         session= DBSession()
         try:
             id = int(key)

myapp/myapp/templates/widget.pt

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:tal="http://xml.zope.org/namespaces/tal">
+<head>
+  <title>The Pyramid Web Application Development Framework</title>
+  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
+  <meta name="keywords" content="python web application" />
+  <meta name="description" content="pyramid web application" />
+  <link rel="shortcut icon" href="${request.static_url('myapp:static/favicon.ico')}" />
+</head>
+<body>
+  <div id="wrap">
+    <div tal:content="structure widget.display()"></div> 
+  </div>
+  <div id="footer">
+    <div class="footer">&copy; Copyright 2008-2011, Agendaless Consulting.</div>
+  </div>
+</body>
+</html>

myapp/myapp/views.py

 
 def view_model(context, request):
     return {'item':context, 'project':'myapp'}
+
+def view_widget(context, request):
+    context.fetch_data(request)
+    return {'widget':context}

myapp/myapp/widgets.py

+import tw2.core
+import tw2.forms
+
+class MovieForm(tw2.forms.FormPage):
+    title = 'Movie'
+    class child(tw2.forms.TableForm):
+        title = tw2.forms.TextField(validator=tw2.core.Required)
+        director = tw2.forms.TextField()
+        genres = tw2.forms.CheckBoxList(
+            options=['Action', 'Comedy', 'Romance', 'Sci-fi'])
+        class cast(tw2.forms.GridLayout):
+            extra_reps = 5
+            character = tw2.forms.TextField()
+            actor = tw2.forms.TextField()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.