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)¶ 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.
-
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
(*args, **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 whose default class is “formColumn”. Example:
Column('form_field_1', 'form_field_2')
-
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
notcss_class
.
Bootstrap¶
-
class
crispy_forms.bootstrap.
PrependedAppendedText
(field, prepended_text=None, appended_text=None, *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.
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
-
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')
-
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
(*fields, **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.’)