watson.form.types

class watson.form.types.Form(name=None, method='post', action=None, detect_multipart=True, validators=None, **kwargs)[source]

<form> management.

The implementation of the form gives the ability to define the fields within the form in a declarative manor.

Example:

from watson.form import fields
class MyForm(Form):
    text = fields.Text(name='text', label='My TextField')

form = MyForm('my_form')
form.text.value = 'Something'

# in view
{% form.open() %}   # <form name="my_form">
{% form.text %}     # <input name="text" type="text" value="Something" />
{% form.text.render_with_label() %} # <label for="text">My TextField</label><input id="text" name="text" type="text" value="Something" />
{% form.close() %}  # </form>
attributes

dict

A list of all attributes on the <form>.

__init__(name=None, method='post', action=None, detect_multipart=True, validators=None, **kwargs)[source]

Inititalize the form and set some default attributes.

Parameters:
  • name (string) – the name of the form
  • method (string) – the http method to use
  • action (string) – the url to submit the form to
  • detect_multipart (boolean) – automatically set multipart/form-data
bind(obj=None, mapping=None, ignored_fields=None, hydrate=True)[source]

Binds an object to the form.

Optionally additional mapping can be specified in order to set values on any of the classes that may exist within the object. If this method is called after the data has been set on the form, then the existing data will be overridden with the attributes on the object unless hydrate is set to false.

Parameters:
  • obj (class|dict) – the class to bind to the form.
  • mapping (dict) – the mapping between the form fields and obj attributes.
  • ignored_fields (list|tuple) – fields to ignore when binding.

Example:

form = ...
user = User(username='test')
form.bind(user)
form.username.value  # 'test'
close(include_http_request=True)[source]

Render the end tag of the form.

If the form has the http_request_method input then include it in the tag by default.

Parameters:include_http_request (boolean) – Whether or not to include the HTTP_REQUEST_METHOD field
data[source]

Returns a dict containing all the field values.

Used as a shorthand method to retrieve data from all the form fields rather than having to access the fields themselves.

invalidate()[source]

Invalidate the data that has been bound on the form.

This is called automatically when data is bound to the form and sets the forms validity to invalid.

is_valid()[source]

Determine whether or not the form and relating values are valid.

Filter all the values on the fields associated with the form, and then validate each field. Will only execute the filter/validation steps if the form has not been previously validated, or has been invalidated.

Returns:boolean value depending on the validity of the form.
open(**kwargs)[source]

Render the start tag of the form.

Any addition kwargs will be used within the attributes.

render(with_tag='div', with_label=True)[source]

Output the entire form as a string.

Called automatically by the __str__ method.

Parameters:
  • with_tag (string) – the tag to be used to separate the elements.
  • with_label (boolean) – render each field with it’s label.
Returns:

A string representation of the form.

class watson.form.types.Multipart(name, method='post', action=None, **kwargs)[source]

Convenience class for forms that should be multipart/form-data.

By default, the Form class will automatically detect whether or not a field is of type file, and convert it to multipart.

__init__(name, method='post', action=None, **kwargs)[source]