Forms

Default Trionyx will generate a form for all fields without any layout. Forms can be created and registered in the forms.py.

trionyx.forms.register(code: Optional[str] = None, model_alias: Optional[str] = None, default_create: Optional[bool] = False, default_edit: Optional[bool] = False, minimal: Optional[bool] = False, create_permission: Optional[str] = None, edit_permission: Optional[str] = None)

Register form for given model_alias, if no model_alias is given the Meta.model is used to generate the model alias.

Parameters:
  • code (str) – Code to identify form
  • model_alias (str) – Alias for a model (if not provided the Meta.model is used)
  • default_create (bool) – Use this form for create
  • default_edit (bool) – Use this form for editing
  • minimal (bool) – Use this form for minimal create
# <app>/forms.py
from trionyx import forms

@forms.register(default_create=True, default_edit=True)
class UserForm(forms.ModelForm):

    class Meta:
        model = User

Layout

Forms are rendered in Trionyx with crispy forms using the bootstrap3 template.

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, Div

class UserUpdateForm(forms.ModelForm):
    # your form fields

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            'email',
            Div(
                Fieldset(
                    'Personal info',
                    'first_name',
                    'last_name',
                    css_class="col-md-6",
                ),
                Div(
                    'avatar',
                    css_class="col-md-6",
                ),
                css_class="row"
            ),
            Fieldset(
                'Change password',
                'new_password1',
                'new_password2',
            ),
        )

Crispy Forms

Standard

class crispy_forms.layout.Layout(*fields)[source]

Form Layout. It is conformed by Layout objects: Fieldset, Row, Column, MultiField, HTML, ButtonHolder, Button, Hidden, Reset, Submit and fields. Form fields have to be strings. Layout objects Fieldset, Row, Column, MultiField and ButtonHolder can hold other Layout objects within. Though ButtonHolder should only hold HTML and BaseInput inherited classes: Button, Hidden, Reset and Submit.

Example:

helper.layout = Layout(
    Fieldset('Company data',
        'is_company'
    ),
    Fieldset(_('Contact details'),
        'email',
        Row('password1', 'password2'),
        'first_name',
        'last_name',
        HTML('<img src="/media/somepicture.jpg"/>'),
        'company'
    ),
    ButtonHolder(
        Submit('Save', 'Save', css_class='button white'),
    ),
)
class crispy_forms.layout.ButtonHolder(*fields, **kwargs)[source]

Layout object. It wraps fields in a <div class=”buttonHolder”>

This is where you should put Layout objects that render to form buttons like Submit. It should only hold HTML and BaseInput inherited objects.

Example:

ButtonHolder(
    HTML(<span style="display: hidden;">Information Saved</span>),
    Submit('Save', 'Save')
)
class crispy_forms.layout.BaseInput(name, value, **kwargs)[source]

A base class to reduce the amount of code in the Input classes.

render(form, form_style, context, template_pack=<SimpleLazyObject: 'bootstrap3'>, **kwargs)[source]

Renders an <input /> if container is used as a Layout object. Input button value can be a variable in context.

class crispy_forms.layout.Submit(*args, **kwargs)[source]

Used to create a Submit button descriptor for the {% crispy %} template tag:

submit = Submit('Search the Site', 'search this site')

Note

The first argument is also slugified and turned into the id for the submit button.

class crispy_forms.layout.Button(*args, **kwargs)[source]

Used to create a Submit input descriptor for the {% crispy %} template tag:

button = Button('Button 1', 'Press Me!')

Note

The first argument is also slugified and turned into the id for the button.

class crispy_forms.layout.Hidden(name, value, **kwargs)[source]

Used to create a Hidden input descriptor for the {% crispy %} template tag.

class crispy_forms.layout.Reset(*args, **kwargs)[source]

Used to create a Reset button input descriptor for the {% crispy %} template tag:

reset = Reset('Reset This Form', 'Revert Me!')

Note

The first argument is also slugified and turned into the id for the reset.

class crispy_forms.layout.Fieldset(legend, *fields, **kwargs)[source]

Layout object. It wraps fields in a <fieldset>

Example:

Fieldset("Text for the legend",
    'form_field_1',
    'form_field_2'
)

The first parameter is the text for the fieldset legend. This text is context aware, so you can do things like:

Fieldset("Data for {{ user.username }}",
    'form_field_1',
    'form_field_2'
)
class crispy_forms.layout.MultiField(label, *fields, **kwargs)[source]

MultiField container. Renders to a MultiField <div>

class crispy_forms.layout.Div(*fields, **kwargs)[source]

Layout object. It wraps fields in a <div>

You can set css_id for a DOM id and css_class for a DOM class. Example:

Div('form_field_1', 'form_field_2', css_id='div-example', css_class='divs')
class crispy_forms.layout.Row(*fields, **kwargs)[source]

Layout object. It wraps fields in a div whose default class is “formRow”. Example:

Row('form_field_1', 'form_field_2', 'form_field_3')
class crispy_forms.layout.Column(*fields, **kwargs)[source]

Layout object. It wraps fields in a div so the wrapper can be used as a column. Example:

Column('form_field_1', 'form_field_2')
Depending on the template, css class associated to the div is formColumn, row, or nothing. For this last case, you

must provide css classes. Example:

Column('form_field_1', 'form_field_2', css_class='col-xs-6',)
class crispy_forms.layout.HTML(html)[source]

Layout object. It can contain pure HTML and it has access to the whole context of the page where the form is being rendered.

Examples:

HTML("{% if saved %}Data saved{% endif %}")
HTML('<input type="hidden" name="{{ step_field }}" value="{{ step0 }}" />')
class crispy_forms.layout.Field(*args, **kwargs)[source]

Layout object, It contains one field name, and you can add attributes to it easily. For setting class attributes, you need to use css_class, as class is a Python keyword.

Example:

Field('field_name', style="color: #333;", css_class="whatever", id="field_name")
class crispy_forms.layout.MultiWidgetField(*args, **kwargs)[source]

Layout object. For fields with MultiWidget as widget, you can pass additional attributes to each widget.

Example:

MultiWidgetField(
    'multiwidget_field_name',
    attrs=(
        {'style': 'width: 30px;'},
        {'class': 'second_widget_class'}
    ),
)

Note

To override widget’s css class use class not css_class.

Bootstrap

class crispy_forms.bootstrap.PrependedAppendedText(field, prepended_text=None, appended_text=None, *args, **kwargs)[source]
class crispy_forms.bootstrap.AppendedText(field, text, *args, **kwargs)[source]
class crispy_forms.bootstrap.PrependedText(field, text, *args, **kwargs)[source]
class crispy_forms.bootstrap.FormActions(*fields, **kwargs)[source]

Bootstrap layout object. It wraps fields in a <div class=”form-actions”>

Example:

FormActions(
    HTML(<span style="display: hidden;">Information Saved</span>),
    Submit('Save', 'Save', css_class='btn-primary')
)
class crispy_forms.bootstrap.InlineCheckboxes(*args, **kwargs)[source]

Layout object for rendering checkboxes inline:

InlineCheckboxes('field_name')
class crispy_forms.bootstrap.InlineRadios(*args, **kwargs)[source]

Layout object for rendering radiobuttons inline:

InlineRadios('field_name')
class crispy_forms.bootstrap.FieldWithButtons(*fields, **kwargs)[source]
class crispy_forms.bootstrap.StrictButton(content, **kwargs)[source]

Layout object for rendering an HTML button:

Button("button content", css_class="extra")
class crispy_forms.bootstrap.Container(name, *fields, **kwargs)[source]

Base class used for Tab and AccordionGroup, represents a basic container concept

class crispy_forms.bootstrap.ContainerHolder(*fields, **kwargs)[source]

Base class used for TabHolder and Accordion, groups containers

first_container_with_errors(errors)[source]

Returns the first container with errors, otherwise returns None.

open_target_group_for_form(form)[source]

Makes sure that the first group that should be open is open. This is either the first group with errors or the first group in the container, unless that first group was originally set to active=False.

class crispy_forms.bootstrap.Tab(name, *fields, **kwargs)[source]

Tab object. It wraps fields in a div whose default class is “tab-pane” and takes a name as first argument. Example:

Tab('tab_name', 'form_field_1', 'form_field_2', 'form_field_3')

Render the link for the tab-pane. It must be called after render so css_class is updated with active if needed.

class crispy_forms.bootstrap.TabHolder(*fields, **kwargs)[source]

TabHolder object. It wraps Tab objects in a container. Requires bootstrap-tab.js:

TabHolder(
    Tab('form_field_1', 'form_field_2'),
    Tab('form_field_3')
)
class crispy_forms.bootstrap.AccordionGroup(name, *fields, **kwargs)[source]

Accordion Group (pane) object. It wraps given fields inside an accordion tab. It takes accordion tab name as first argument:

AccordionGroup("group name", "form_field_1", "form_field_2")
class crispy_forms.bootstrap.Accordion(*args, **kwargs)[source]

Accordion menu object. It wraps AccordionGroup objects in a container:

Accordion(
    AccordionGroup("group name", "form_field_1", "form_field_2"),
    AccordionGroup("another group name", "form_field")
)
class crispy_forms.bootstrap.Alert(content, dismiss=True, block=False, **kwargs)[source]

Alert generates markup in the form of an alert dialog

Alert(content=’<strong>Warning!</strong> Best check yo self, you’re not looking too good.’)
class crispy_forms.bootstrap.UneditableField(field, *args, **kwargs)[source]

Layout object for rendering fields as uneditable in bootstrap

Example:

UneditableField('field_name', css_class="input-xlarge")
class crispy_forms.bootstrap.InlineField(*args, **kwargs)[source]

Trionyx

TimePicker

class trionyx.forms.layout.TimePicker(field, **kwargs)[source]

Timepicker field renderer