James Gardner: Home > Work > Code > FormBuild > 3.0.1 > API Reference and Examples

FormBuild v3.0.1 documentation

API Reference and Examples

class formbuild.Form(value=None, error=None, flow=None, option=None, checked=None, table_class='formbuild')
start(action='', method='post', enctype=None, attributes=None)

Open a form tag which submits via the POST method. You must close the form yourself.

action
The URL the form will submit to. Defaults to '' so that the form submits to the current URL
method
Can be post or get and affects the HTTP method used to submit the form.
enctype
The encoding type, only usually set if your form contains fields for uploading a file.
attributes
A dictionary containing other HTML attributes (apart from action, method and enctype)

Here are some examples:

>>> from formbuild import Form
>>> form = Form()
>>> print form.start("/submit")
<form action="/submit" method="post">
>>> print form.start("/submit", method="get")
<form action="/submit" method="get">

If your form contains file fields you must use method='post (the default) and also set the enctype attribute to contain the value "multipart/form-data" otherwise your browser will submit the filename instead of the file content. Here’s an example:

>>> print form.start(
...     "/submit",
...     "post", 
...     enctype="multipart/form-data",
... )
<form action="/submit" method="post" enctype="multipart/form-data">
class formbuild.Form(value=None, error=None, flow=None, option=None, checked=None, table_class='formbuild')
end(hidden_field_names=None)

End a form, adding hidden fields for any values with names in the hidden_field_names list.

>>> form = Form()
>>> print form.end()
</form>
>>> form = Form(value={'firstname': 'james', 'surname': 'garnder'})
>>> print form.end(hidden_field_names=['firstname', 'surname'])
<input type="hidden" name="firstname" value="james" />
<input type="hidden" name="surname" value="garnder" />
</form>
Form.end(hidden_field_names=None)

End a form, adding hidden fields for any values with names in the hidden_field_names list.

>>> form = Form()
>>> print form.end()
</form>
>>> form = Form(value={'firstname': 'james', 'surname': 'garnder'})
>>> print form.end(hidden_field_names=['firstname', 'surname'])
<input type="hidden" name="firstname" value="james" />
<input type="hidden" name="surname" value="garnder" />
</form>

First let’s consider the form methods:

Form.start()

Open a form tag which submits via the POST method. You must close the form yourself.

action
The URL the form will submit to. Defaults to '' so that the form submits to the current URL
method
Can be post or get and affects the HTTP method used to submit the form.
enctype
The encoding type, only usually set if your form contains fields for uploading a file.
attributes
A dictionary containing other HTML attributes (apart from action, method and enctype)

Here are some examples:

>>> from formbuild import Form
>>> form = Form()
>>> print form.start("/submit")
<form action="/submit" method="post">
>>> print form.start("/submit", method="get")
<form action="/submit" method="get">

If your form contains file fields you must use method='post (the default) and also set the enctype attribute to contain the value "multipart/form-data" otherwise your browser will submit the filename instead of the file content. Here’s an example:

>>> print form.start(
...     "/submit",
...     "post", 
...     enctype="multipart/form-data",
... )
<form action="/submit" method="post" enctype="multipart/form-data">
James Gardner: Home > Work > Code > FormBuild > 3.0.1 > API Reference and Examples