Module provides views for basic form rendering and processing.
Note
The following packages must be installed:
Bases: flask_velox.mixins.forms.FormMixin
Class implements flask_velox.mixins.forms.FormMixin allowing rendering of Flask-WTF forms.
Note
Context pased to template:
Example
from flask.ext.velox.views.forms import FormView
from yourapp.forms import MyForm
class MyView(FormView):
template = 'form.html'
form = MyForm
x.__delattr__(‘name’) <==> del x.name
default object formatter
x.__getattribute__(‘name’) <==> x.name
x.__hash__() <==> hash(x)
helper for pickle
helper for pickle
x.__repr__() <==> repr(x)
x.__setattr__(‘name’, value) <==> x.name = value
size of object in memory, in bytes
x.__str__() <==> str(x)
Adds a new element to the context.
Parameters: |
|
---|---|
Returns: | The new context |
Return type: | dict |
Converts the class into an actual view function that can be used with the routing system. Internally this generates a function on the fly which will instantiate the View on each request and call the dispatch_request() method on it.
The arguments passed to as_view() are forwarded to the constructor of the class.
Removes an element from the context dictionary.
Example
>>> class FooView(ContextMixin):
... context = {
... 'foo': 'bar',
... }
...
>>> view = FooView()
>>> view.del_context('foo')
{}
Parameters: | key (str) – The context key name |
---|---|
Returns: | Success or Failure |
Return type: | bool |
Returns: | The new context |
Return type: | dict |
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')
Handle HTTP GET requets using Flask MethodView rendering a single html template.
Returns: | Rendered template |
---|---|
Return type: | str |
Propety method which returns the current context.
Returns: | Current context value |
---|---|
Return type: | dict |
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 |
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 |
Merge the passed dictionary into the current _context
Parameters: | subject (dict) – The dict to merge into _context |
---|
Example
>>> class FooView(ContextMixin):
... context = {
... 'foo': 'bar',
... }
...
>>> view = FooView()
>>> view.merge_context({'hello': 'world'})
>>> view.get_context()
{
'hello': 'world',
'foo': 'bar'
}
Returns: | The new context |
---|---|
Return type: | dict |
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 |
Renders a template. This method will attempt to pass context to the template but if the context attribute does not exist then an empty dict will be passed to the render_template method.
Returns: | Rendered template |
---|---|
Return type: | str |
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 |
Overwrites the existing context with the provided new context value.
Example
>>> class FooView(ContextMixin):
... context = {
... 'foo': 'bar',
... }
...
>>> view = FooView()
>>> view.update_context({'hello': 'world'})
{
'hello': 'world'
}
Parameters: | new (dict) – Value to update context too |
---|---|
Returns: | The new context |
Return type: | dict |
Bases: flask_velox.mixins.forms.MultiFormMixin
Class implements flask_velox.mixins.forms.MultiFormMixin allowing rendering of multiple Flask-WTF forms.
Note
Context pased to template:
Example
from flask.ext.velox.views.forms import MultiFormView
from yourapp.forms import FooForm, BarForm
class MyView(MultiFormView):
template = 'forms.html'
forms = [FooForm, BarForm]
x.__delattr__(‘name’) <==> del x.name
default object formatter
x.__getattribute__(‘name’) <==> x.name
x.__hash__() <==> hash(x)
helper for pickle
helper for pickle
x.__repr__() <==> repr(x)
x.__setattr__(‘name’, value) <==> x.name = value
size of object in memory, in bytes
x.__str__() <==> str(x)
Adds a new element to the context.
Parameters: |
|
---|---|
Returns: | The new context |
Return type: | dict |
Converts the class into an actual view function that can be used with the routing system. Internally this generates a function on the fly which will instantiate the View on each request and call the dispatch_request() method on it.
The arguments passed to as_view() are forwarded to the constructor of the class.
Removes an element from the context dictionary.
Example
>>> class FooView(ContextMixin):
... context = {
... 'foo': 'bar',
... }
...
>>> view = FooView()
>>> view.del_context('foo')
{}
Parameters: | key (str) – The context key name |
---|---|
Returns: | Success or Failure |
Return type: | bool |
Returns: | The new context |
Return type: | dict |
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')
Handle HTTP GET requets using Flask MethodView rendering a single html template.
Returns: | Rendered template |
---|---|
Return type: | str |
Propety method which returns the current context.
Returns: | Current context value |
---|---|
Return type: | dict |
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 |
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 |
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 |
Merge the passed dictionary into the current _context
Parameters: | subject (dict) – The dict to merge into _context |
---|
Example
>>> class FooView(ContextMixin):
... context = {
... 'foo': 'bar',
... }
...
>>> view = FooView()
>>> view.merge_context({'hello': 'world'})
>>> view.get_context()
{
'hello': 'world',
'foo': 'bar'
}
Returns: | The new context |
---|---|
Return type: | dict |
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 |
Renders a template. This method will attempt to pass context to the template but if the context attribute does not exist then an empty dict will be passed to the render_template method.
Returns: | Rendered template |
---|---|
Return type: | str |
Updates context to contain extra 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 |
Overwrites the existing context with the provided new context value.
Example
>>> class FooView(ContextMixin):
... context = {
... 'foo': 'bar',
... }
...
>>> view = FooView()
>>> view.update_context({'hello': 'world'})
{
'hello': 'world'
}
Parameters: | new (dict) – Value to update context too |
---|---|
Returns: | The new context |
Return type: | dict |