Form related widgets#

class BaseCheckboxInputBlock(checked: bool = False, **kwargs)[source]#

Bases: InputBlock

A block for rendering a bare <input type="checkbox"> element.

Example

>>> block = BaseCheckboxInputBlock(name='my-checkbox', value=1)
>>> block = BaseCheckboxInputBlock(name='my-checkbox', value=1, checked=true)
Keyword Arguments:

checked – if True, render the checkbox as checked

class CheckboxInputBlock(label_text: Optional[str] = None, bold: Optional[bool] = None, input_name: Optional[str] = None, value: Optional[str] = None, checked: bool = False, **kwargs)[source]#

Bases: Block

A block for rendering a <input type="checkbox"> element with a label as a Boostrap 5 check.

Example

>>> block = CheckboxInputBlock(label_text='My Checkbox', name='my-checkbox', value=1)
Keyword Arguments:
  • label_text – the text to use for the label

  • bold – if True, make the label text be bold

  • input_name – the value of the name attribute

  • value – the value of the value attribute

  • checked – if True, render the checkbox as checked

bold: bool = True#

if True, make the label text be bold

input_name: Optional[str] = None#

the value of the name attribute

label_text: Optional[str] = None#

the text to use for the label

value: Optional[str] = None#

the value of the value attribute

class CrispyFormWidget(*blocks, form: Optional[Form] = None, **kwargs)[source]#

Bases: Block

A widget that displays a Crispy Form widget. This is specifically a Crispy Form widget because it uses the {% crispy %} template tag to render the form.

Note

If the form is present in the template context already, (because your view inserted it for you) you don’t need to supply the form keyword argument – CrispyFormWidget will pick it up automatically.

Keyword Arguments:

form – A crispy form to display. If none is specified, it will be assumed that form is specified elsewhere in the context.

get_context_data(*args, **kwargs)[source]#

Update the template context dictionary used when rendering this block.

Keyword Arguments:

**kwargs – the current context dictionary

Returns:

The updated context dictionary

block: str = 'wildewidgets-crispy-form-widget'#

block is the official wildewidgets name of the block; it can’t be changed by constructor kwargs

template_name: str = 'wildewidgets/crispy_form_widget.html'#

The name of the template to use to render this widget

class HiddenInputBlock(**kwargs)[source]#

Bases: InputBlock

A block for rendering a <input type="hidden">.

Example

>>> block = HiddenInputBlock(name='my-checkbox', value=1)
class InputBlock(input_type: Optional[str] = None, input_name: Optional[str] = None, value: Optional[str] = None, **kwargs)[source]#

Bases: Block

A block for rendering a simple <input> element.

Example

>>> block = InputBlock(input_type='checkbox', input_name='my-checkbox', value=1)
Keyword Arguments:
  • input_type – the value of the type attribute

  • input_name – the value of the name attribute

  • value – the value of the value attribute

empty: bool = True#

If True, this block uses no close tag

tag: str = 'input'#

The name of the HTML element to use as our tag, e.g. div

class LabelBlock(text: str, for_input: Optional[str] = None, bold: Optional[bool] = None, color: str = 'secondary', **kwargs)[source]#

Bases: Block

A <label>

Parameters:

text – the text for the label

Keyword Arguments:
  • bold – if True, make the label text be bold

  • color – the bootstrap/tabler color to assign to the label

  • for_input – the CSS id of the input this describes

bold: bool = True#

If True, make the label text be bold

for_input: Optional[str] = None#

The CSS id of the input this describes

tag: str = 'label'#

The name of the HTML element to use as our tag, e.g. div

class ToggleSwitchInputBlock(**kwargs)[source]#

Bases: CheckboxInputBlock

A block for rendering a <input type="checkbox"> element with a label as a Bootstrap 5 switch.

Example

>>> block = ToggleSwitchInputBlock(label_text='My Checkbox', name='my-checkbox', value=1)
blocks: List[Union[str, Widget]]#

The internal list of blocks that are this block’s content

class ToggleableManyToManyFieldBlock(instance: Model, field_name: Optional[str] = None, form_class: Optional[Type[Form]] = None, form_action: Optional[str] = None, **kwargs)[source]#

Bases: CardWidget

This is a block that allows you to manage a many to many relationship between a model and a related model as a list of toggleable items. The block lists all available items, showing which ones are selected on our model instance as checked toggle switches. It also offers a search box to search for items in the list, and a control to hide unselected items.

This is mostly appropriate for relationships between a model and a list-of-values type lookup table (e.g. categories, tags, classifiers, etc.).

Keyword Args:

form_class#

The form class to instantiate to handle our multiple select

alias of ToggleableManyToManyFieldForm

get_form(instance: Model, field_name: str, form_action: str) Form[source]#

Return the form to render into our widget, bound to field_name on instance.

Parameters:
  • instance – the model instance to which to bind our form

  • field_name – the name of our many-to-many field on our model

  • form_action – the URL to which to POST our form

Returns:

An instance of form_class bound to instance.

get_header() Block[source]#

Get our card header. This consists of a toggle switch which hides/shows unselected items and a search input that allows the user to search for items.

Returns:

The card header block.

classmethod get_urlpatterns(url_prefix: Optional[str] = None, url_namespace: Optional[str] = None) List[URLPattern][source]#

Build a view that will service this block and return a django.urls.URLPattern for that view that you can add to your urlpatterns.

Example:

from typing import List
from django.urls import path, URLPattern

from wildewidgets import ToggleableManyToManyFieldBlock

from .views import HomeView
from .models import MyModel

class TagsSelectorWidget(ToggleableManyToManyFieldBlock):

    model = MyModel
    field_name = 'tags'

app_name: str = "myapp"

urlpatterns: List[URLPattern] = [
    path('', HomeView.as_view(), name='home'),
]
urlpatterns += TagsSelectorWidget.get_urlpatterns(url_namespace=app_name)

Important

In order for this to work, you must have subclassed ToggleableManyToManyFieldBlock and defined both the model and field_name attributes.

Keyword Arguments:
  • url_prefix – a prefix to the path we will build for our view

  • url_namespace – the namespace for our url pattern. We’ll set our url_namespace from this

Returns:

A list of urlpatterns for a view suitable for this block

field_name: Optional[str] = None#

The name of the related field on our model instance for which we want to build this widget

property filter_id: str#

Return the CSS id we should use for the filter items search input.

form_action: Optional[str] = None#

The URL to which to POST this form

property form_id: str#

Return the CSS id we should use for our form.

model: Optional[Type[Model]] = None#

The model this widget will be used with. This is only used by our as_v

name: str = 'toggle-form-block'#

The CSS class that will be added to this element to as an identifier for this kind of block

property show_all_switch_id: str#

Return the CSS id we should for our “Hide unselected” toggle.

url_namespace: Optional[str] = None#

The URL namespace to use for our view name. This should be set to the app_name set in the urls.py where this viewset’s patterns will be added to urlpatterns.

url_prefix: str = ''#

The path prefix to use to root view.