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 boldinput_name – the value of the
name
attributevalue – the value of the
value
attributechecked – if
True
, render the checkbox as checked
- 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
- 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
attributeinput_name – the value of the
name
attributevalue – the value of the
value
attribute
- 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 boldcolor – the bootstrap/tabler color to assign to the label
for_input – the CSS id of the input this describes
- 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)
- 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
oninstance
.- 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 toinstance
.
- 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 yoururlpatterns
.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 themodel
andfield_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
- 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