class Meta — WTForms Documentation (3.2.x)

# class Meta

the class Meta paradigm allows WTForms features to be customized, and even new behaviors to be introduced. It also supplies a place where configuration for any complementary modules can be done.

Typical usage looks something like:

    class MyForm(Form):
        class Meta:
            csrf = True
            locales = ('en_US', 'en')

        name = StringField(...)
        # and so on...

For the majority of users, using a class Meta is mostly going to be done for customizing options used by the default behaviors, however for completeness the entire API of the Meta interface is shown here.

class wtforms.meta.DefaultMeta

  This is the default Meta class which defines all the default values and therefore also the ‘API’ of the class Meta interface.

  Configuration

  csrf = False

    Setting `csrf` to True will enable CSRF for the form. The value can also be overridden per-instance via instantiation-time customization (for example, if csrf needs to be turned off only in a special case)

        form = MyForm(request.form, meta={'csrf': False})

  csrf_class = None

    If set, this is a class which is used to implement CSRF protection. Read the CSRF Documentation to get more information on how to use.

  csrf_field_name = 'csrf_token'

    The name of the automatically added CSRF token field.

  locales = False

    Setting to a sequence of strings specifies the priority order of locales to try to find translations for built-in messages of WTForms.

    If the value is False, then strings are not translated (the translations provider is replaced with a dummy provider)

    example:

        locales = ('fr_FR', 'fr')

    Also see Internationalization (i18n) for more information.

  cache_translations = True

    If True (the default) then cache translation objects. The default cache is done at class-level so it’s shared with all class Meta.

  Advanced Customization

  Usually, you do not need to override these methods, as they provide core behaviors of WTForms.

  build_csrf(form)

    Build a CSRF implementation. This is called once per form instance.

    The default implementation builds the class referenced to by `csrf_class` with zero arguments. If csrf_class is `None`, will instead use the default implementation `wtforms.csrf.session.SessionCSRF`.

    Parameters:

      form – The form.

    Returns:

      A CSRF implementation.

  get_translations(form)

    Override in subclasses to provide alternate translations factory. See the i18n documentation for more.

    Parameters:

      form – The form.

    Returns:

      An object that provides gettext() and ngettext() methods.

  bind_field(form, unbound_field, options)

    bind_field allows potential customization of how fields are bound.

    The default implementation simply passes the options to `UnboundField.bind()`.

    Parameters:

      - form – The form.

      - unbound_field – The unbound field.

      - options – A dictionary of options which are typically passed to the field.

    Returns:

      A bound field

  wrap_formdata(form, formdata)

    wrap_formdata allows doing custom wrappers of WTForms formdata.

    The default implementation detects webob-style multidicts and wraps them, otherwise passes formdata back un-changed.

    Parameters:

      - form – The form.

      - formdata – Form data.

    Returns:

      A form-input wrapper compatible with WTForms.

  render_field(field, render_kw)

    render_field allows customization of how widget rendering is done.

    The default implementation calls `field.widget(field, **render_kw)`