Edit views

Views

BaseFormView

class flask_views.edit.BaseFormView

Base view for displaying a form.

This class inherits from:

This class implements all logic for processing forms, but does not include rendering responses. See FormView for an usage example.

FormView

class flask_views.edit.FormView

View for displaying a form, including rendering of template.

This class inherits from:

This class implements all logic for displaying and processing form submissions, including rendering of templates.

Usage example:

class ContactFormView(FormView):
    form_class = ContactForm
    template_name = 'contact_form.html'

    def form_valid(self, form):
        # Do something with the submitted form data
        return super(ContactFormView, self).form_valid(form)

    def get_success_url(self):
        return url_for('contact.form')

An instance of the form class will be available in the template context under the form variable.

Mixins

FormMixin

class flask_views.edit.FormMixin

Mixin for handling form submissions.

form_class = None

Set this to the form class (WTForms) you want to use.

form_invalid(form)

Handle invalid form submission.

This will render the response with the current form in the context so that the errors can be displayed to the user.

Parameters:form – Instance of the form class.
Returns:Response containing the form in the context.
form_valid(form)

Handle valid form submission.

This redirects the user to the URL returned by get_success_url(). You want to override this method for processing the submitted form data.

Parameters:form – Instance of the form.
Returns:Redirect to URL returned by get_success_url().
get_context_data(**kwargs)

Return a dict containing the context data.

Returns:A dict containing the given keyword arguments.
get_form()

Return an instance of the form class.

Returns:Instance form_class.
get_form_kwargs()

Return parameters for creating the form instance.

Returns:A dict containing the arguments for creating the form instance.
get_initial()

Return initial form data.

Override this method when the initial form data should be generated dynamically. By default this returns initial.

Returns:initial.
get_success_url()

Return success URL.

Override this method when the success URL depends on the submitted form data or when it should be generated during an request. By default, this returns success_url.

Returns:success_url.
initial = {}

Set this to the initial data for form fields. Example:

initial = {
    'title': 'Initial title value',
    'body': 'Initial body value.',
}
success_url = None

Set this to the URL the user should be redirected to after a successful form submission.

ProcessFormMixin

class flask_views.edit.ProcessFormMixin

Mixin for processing form data on GET and POST requests.

get(*args, **kwargs)

Handler for GET requests.

This will call render_to_response with an instance of the form as form in the context data.

Returns:Output of render_to_response method implementation.
post(*args, **kwargs)

Handler for POST requests.

On a valid form submission, this will dispatch the request to the form_valid method, else it is dispatched to form_invalid.

Returns:Output of form_valid or form_invalid.

Table Of Contents

Related Topics

This Page