I am a web form.
In order to use me, you probably want to set self.formFields (or override
getFormFields ) and override process . In order to demonstrate how this
is done, here is a small sample Form subclass:
| from twisted.web import widgets
| class HelloForm(widgets.Form):
| formFields = [
| ['string', 'Who to greet?', 'whoToGreet', 'World',
| 'This is for choosing who to greet.'],
| ['menu', 'How to greet?', 'how', [('cheerfully', 'with a smile'),
| ('sullenly', 'without enthusiasm'),
| ('spontaneously', 'on the spur of the moment')]]
| 'This is for choosing how to greet them.']
| def process(self, write, request, submit, whoToGreet, how):
| write('The web wakes up and %s says, "Hello, %s!"' % (how, whoToGreet))
If you load this widget, you will see that it displays a form with 2 inputs
derived from data in formFields. Note the argument names to 'process':
after write and request , they are the same as the 3rd elements (Input
Name parameters) of the formFields list.
Methods
|
|
_doProcess
format
formatError
getFormFields
getFormID
process
shouldProcess
stream
tryAgain
|
|
_doProcess
|
_doProcess (
self,
form,
write,
request,
)
(internal) Prepare arguments for self.process.
Exceptions
|
|
FormInputError("missing field %s." % repr( inputName ) )
FormInputError("multiple values for field %s." % repr( inputName ) )
FormInputError("unknown fields: %s" % repr( args ) )
|
|
|
format
|
format (
self,
form,
write,
request,
)
I display an HTML FORM according to the result of self.getFormFields.
|
|
formatError
|
formatError ( self, error )
Format an error message.
By default, this will make the message appear in red, bold italics.
|
|
getFormFields
|
getFormFields (
self,
request,
fieldSet=None,
)
I return a list of lists describing this form.
This information is used both to display the form and to process it.
The list is in the following format:
| [['Input Type', 'Display Name', 'Input Name', 'Input Value', 'Description'],
| ['Input Type 2', 'Display Name 2', 'Input Name 2', 'Input Value 2', 'Description 2']
| ...]
Valid values for Input Type are:
'hidden': a hidden field that contains a string that the user won't change
'string': a short string
'int': an integer
'text': a longer text field, suitable for entering paragraphs
'menu': an HTML SELECT input, a list of choices
'multimenu': an HTML SELECT input allowing multiple choices
'checkgroup': a group of checkboxes
'password': a string field where the contents are not visible as the user types
'file': a file-upload form (EXPERIMENTAL)
Display Name is a descriptive string that will be used to
identify the field to the user.
The Input Name must be a legal Python identifier that describes both
the value's name on the HTML form and the name of an argument to
self.process() .
The Input Value is usually a string, but its value can depend on the
Input Type . int it is an integer, menu it is a list of pairs of
strings, representing (value, name) pairs for the menu options. Input
value for checkgroup should be a list of (inputName , Display
Name , checked ) triplets.
The Description field is an (optional) string which describes the form
item to the user.
If this result is statically determined for your Form subclass, you can
assign it to FormSubclass.formFields; if you need to determine it
dynamically, you can override this method.
Note: In many cases it is desirable to use user input for defaults in
the form rather than those supplied by your calculations, which is what
this method will do to self.formFields. If this is the case for you,
but you still need to dynamically calculate some fields, pass your
results back through this method by doing:
| def getFormFields(self, request):
| myFormFields = [self.myFieldCalculator()]
| return widgets.Form.getFormFields(self, request, myFormFields)
|
|
getFormID
|
getFormID ( self )
Override me: I disambiguate between multiple forms of the same type.
In order to determine which form an HTTP POST request is for, you must
have some unique identifier which distinguishes your form from other
forms of the same class. An example of such a unique identifier would
be: on a page with multiple FrobConf forms, each FrobConf form refers
to a particular Frobnitz instance, which has a unique id(). The
FrobConf form's getFormID would probably look like this:
| def getFormID(self):
| return str(id(self.frobnitz))
By default, this method will return None, since distinct Form instances
may be identical as far as the application is concerned.
|
|
process
|
process (
self,
write,
request,
submit,
**kw,
)
Override me: I process a form.
I will only be called when the correct form input data to process this
form has been received.
I take a variable number of arguments, beginning with write ,
request , and submit . write is a callable object that will append
a string to the response, request is a twisted.web.request.Request
instance, and submit is the name of the submit action taken.
The remainder of my arguments must be correctly named. They will each be named after one of the
|
|
shouldProcess
|
shouldProcess ( self, request )
|
|
stream
|
stream (
self,
write,
request,
)
Render the results of displaying or processing the form.
|
|
tryAgain
|
tryAgain (
self,
err,
req,
)
Utility method for re-drawing the form with an error message.
This is handy in forms that process Deferred results. Normally you can
just raise a FormInputError() and this will happen by default.
|
|