Mixin classes for handeling form rendering and processing.
Note
The following packages must be installed:
Bases: flask_velox.mixins.context.ContextMixin, flask_velox.mixins.template.TemplateMixin
Base Form Mixin class, defines some standard methods required for both single and multi forms.
Raw Flask url rule, e.g: some.url.rule
Flask url rule for form submit action e.g: ‘some.url.rule’
Override this method to call a flask flash method. By default this method does nothing.
Example
class MyView(FromMixin):
form = Form
def flash(self):
flash('Message', 'success')
Returns raw redirect url rule to be used in url_for. The redirect_url_rule must be defined else NotImplementedError will be raised.
Returns: | Raw flask url endpoint |
---|---|
Return type: | str |
Raises: | NotImplementedError – If redirect_url_rule is not defined |
Returns a submit url rule for usage in generating a submit form action url. Defaults to the views current url rule endpoint but can be overridden by defining submit_url_rule on the view class.abs
Returns: | Raw flask url rule endpoint |
---|---|
Return type: | str |
Instantiates form if instance does not already exisst. Override this method to tailor form instantiation.
Parameters: |
|
---|---|
Returns: | Instantiated form |
Return type: | object |
Handle HTTP POST requets using Flask MethodView rendering a single html template.
Returns: | Rendered template |
---|---|
Return type: | str |
Returns the url to a redirect endpoint, when the form is valid and the callback is called.
See also
Parameters: | **kwargs – Arbitrary keyword arguments passed to Flask.url_for |
---|---|
Returns: | Generated url |
Return type: | str or None |
Overrides set_context to set extra context variables.
See also
Note
Adds the following extra context variables:
Returns the url to a submit endpoint, this is used to render a link in forms actions:
<form action="{{ submit_url() }}" method="POST">
...
</form>
See also
Parameters: | **kwargs – Arbitrary keyword arguments passed to Flask.url_for |
---|---|
Returns: | Generated url |
Return type: | str or None |
Called on successful form validation, by default this will perform a redirect if redirect_url_rule is defined. Override this method to perform any custom actions on successful form validation.
Returns: | Redirects request to somewhere else |
---|---|
Return type: | werkzeug.wrappers.Response |
Bases: flask_velox.mixins.forms.BaseFormMixin
Renders and validates a single Form.
Example
1 2 3 4 5 | from flask.velox.mixins.forms import FormMixin
from yourapp.forms import MyForm
class MyView(FormMixin):
form_class = MyForm
|
An uninstantiated WTForm class
Returns an instantiated WTForm class.
Returns: | Instantiated form |
---|---|
Return type: | object |
Returns defined form_class or riases NotImplementedError
Returns: | Uninstantiated WTForm class |
---|---|
Return type: | class |
Raises: | NotImplementedError – If form_class is not defined |
Overrides set_context to set extra context variables.
See also
Note
Adds the following extra context variables:
Bases: flask_velox.mixins.forms.BaseFormMixin
Mixin allows rendering of multiple forms in a single template, each form is submit individually and validated individually but to the same endpoint.
Example
1 2 3 4 5 6 7 8 | from flask.velox.mixins.forms import MultiFormMixin
from yourapp.forms import Form1, Form2
class MyView(MultiFormMixin):
forms = [
('Form 1', Form1)
('Form 2', Form2)
]
|
Get the submit form if one exists else return None, this allows us to get the correctly submit form to validate against and populate objects if required.
A hidden field must be included in each <form> block named form containing the value of the forms prefix. This is the forms uniqie identifier and is used to obtain the submit form.
Returns: | Submit form object or None if submit form not found |
---|---|
Return type: | obj or None |
Return list of tuples of form classes, the tuple should contain a human readable name and a form class defined in forms attribute. If not set NotImplementedError is risen.
Returns: | Tuples containing human name and form class |
---|---|
Return type: | list |
Raises: | NotImplementedError – forms attribute is not defined |
Instantiates forms set in forms attribute giving each form an individual prefix and storing each form in a dict using its prefix as the key.
Returns: | Instantiated forms with form prefix as key and a tuple containing
the human readable form name and form object:{
'form1': ('Foo Form', <object>),
'form1': ('Bar Form', <object>)
}
|
---|---|
Return type: | collections.OrderedDict |
If the form has been submit run the validate on submit method and call the success callback if the form is valid.
Parameters: |
|
---|---|
Returns: | If the form was submit§ |
Return type: | bool |
Updates context to contain extra variables.
See also
Note
Adds the following extra context variables: