Makes easier to create AJAX forms in TurboGears2
Project description
About Ajax Forms
tgext.ajaxforms makes quick and easy to create ajax loaded and submitted forms in TurboGears2 applications.
To make a form ajax based just apply the ajaxforms.ajaxloaded decorator to any ToscaWidgets form and declare a ajaxurl variable to specify the method that loads the form. The method can be automatically generated or hand-written. By default ajax validation is automatically supported.
Installing
tgext.ajaxform can be installed both from pypi or from bitbucket:
easy_install tgext.ajaxforms
should just work for most of the users
Making an Ajax Form
The form itself can be any ToscaWidgets form, the only required additions are to apply the @ajaxloaded decorator to the form itself and specify the ajaxurl of the form that will point to the related controller method:
from tgext.ajaxforms import ajaxloaded @ajaxloaded class TestForm(twf.TableForm): class fields(WidgetsList): name = twf.TextField('Name', validator=validators.String(not_empty=True)) surname = twf.TextField('Surname', validator=validators.String(not_empty=True)) ajaxurl = '/form_show' action = '/form_submit' submit_text = "GO" test_form = TestForm()
Showing The Form
To show the form it is necessary to add a controller method bound to the ajaxurl parameter of the form that will display the form itself and that will be used by ajaxform each time that it has to display the form. For most cases this method can be generated by using the ajaxform call. Also you have to create a page where the form will be loaded.
For example to show the form in the index page having ajaxurl = ‘/form_show’:
from tgext.ajaxforms import ajaxform class RootController(BaseController): form_show = ajaxform(test_form) @expose('myapp.templates.index') def index(self): return dict(form=test_form)
The myapp.templates.index template should look the usual template that you would use to display a ToscaWidgets based form:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/" xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include href="master.html" /> <head> <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/> <title>Ajax Form Test Example</title> </head> <body> <div id="myform"> ${form(name='HI')} </div> </body> </html>
Handling Submit and Validation
Submit and validation should look the same that you would use for any ToscaWidgets form, simply using the ajaurl bound method as an error_handler:
class RootController(BaseController): @expose() @validate(test_form, error_handler=form_show) def form_submit(self, **kw): return 'Thanks: {name} {surname}'.format(**kw)
Complete Example
myapp.templates.index:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/" xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include href="master.html" /> <head> <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/> <title>Ajax Form Test Example</title> </head> <body> <div id="myform"> ${form(name='HI')} </div> </body> </html>
myapp.controllers.root:
from tgext.ajaxforms import ajaxloaded, ajaxform @ajaxloaded class TestForm(twf.TableForm): class fields(WidgetsList): name = twf.TextField('Name', validator=validators.String(not_empty=True)) surname = twf.TextField('Surname', validator=validators.String(not_empty=True)) ajaxurl = '/form_show' action = '/form_submit' submit_text = "GO" test_form = TestForm() class RootController(BaseController): form_show = ajaxform(test_form) @expose('myapp.templates.index') def index(self): return dict(form=test_form) @expose() @validate(test_form, error_handler=form_show) def form_submit(self, **kw): return 'Thanks: {name} {surname}'.format(**kw)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file tgext.ajaxforms-0.1.tar.gz
.
File metadata
- Download URL: tgext.ajaxforms-0.1.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2eb84dc4f35be4e479cc2e9d9b03ab3605b2fc6896f588d8d576629d6c825e5d |
|
MD5 | bf05469bf4db45a2338eee49de983dd8 |
|
BLAKE2b-256 | 0c4eaf1491ecf3c0567988a2dd068843559a84c6e26f22dfa8cef79aaf59aa7a |