djblets.configforms.forms¶
Base support for configuration forms.
- class ConfigPageForm(page, request, user, *args, **kwargs)¶
Bases:
Form
Base class for a form on a ConfigPage.
Consumers can subclass ConfigPageForm and register it on a
djblets.configforms.pages.ConfigPage
. It will be shown whenever the user navigates to that page.A form will generally be shown as a box with a title and a save button, though this is customizable.
A standard form presents fields that can be filled out and posted. More advanced forms can supply their own template or even their own JavaScript models, views, and CSS.
- form_id: Optional[str] = None¶
The unique ID of the form.
This must be unique across all ConfigPages at a given URL.
- save_label: Optional[Union[str, StrPromise]] = 'Save'¶
The label for the save button.
This can be set to
None
to disable the button.
- template_name = 'configforms/config_page_form.html'¶
The template used to render the form.
- js_model_class: Optional[str] = None¶
The optional Backbone model used for the configuration form state.
- __init__(page, request, user, *args, **kwargs)¶
Initialize the form.
- Parameters:
page (
ConfigPage
) – The page this form resides on.request (
HttpRequest
) – The HTTP request from the client.user (
User
) – The user who is viewing the page.
- set_initial(field_values)¶
Set the initial fields for the form based on provided data.
This can be used during
load()
to fill in the fields based on data from the database or another source.- Parameters:
field_values (
dict
) – The initial field data to set on the form.
- is_visible()¶
Return whether the form should be visible.
This can be overridden to hide forms based on certain criteria.
- Returns:
True
if the form should be rendered on the page (default), orFalse
otherwise.- Return type:
- get_js_model_data()¶
Return data to pass to the JavaScript Model during instantiation.
If
js_model_class
is provided, the data returned from this function will be provided to the model when constructed.- Returns:
A dictionary of attributes to pass to the Model instance. By default, it will be empty.
- Return type:
- get_js_view_data()¶
Return data to pass to the JavaScript View during instantiation.
If
js_view_class
is provided, the data returned from this function will be provided to the view when constructed.- Returns:
A dictionary of options to pass to the View instance. By default, it will be empty.
- Return type:
- render()¶
Render the form to a string.
template_name
will be used to render the form. The template will be passedform
(this form’s instance) andpage
(the parentConfigPage
).Subclasses can override this to provide additional rendering logic.
- Returns:
The rendered form as HTML.
- Return type:
- get_extra_context()¶
Return extra rendering context.
Subclasses can override this to provide additional rendering context.
- Returns:
The additional rendering context. By default, it is empty.
- Return type:
- load()¶
Load data for the form.
By default, this does nothing. Subclasses can override this to load data into the fields based on data from the database or from another source.
- save()¶
Save the form data.
Subclasses must override this to save data from the fields into the database.
- Returns:
An HTTP response to return from the view after saving, or
None
to be returned to theConfigPagesView
.- Return type:
- __annotations__ = {'css_bundle_names': 'List[str]', 'form_id': 'Optional[str]', 'form_title': 'Optional[StrOrPromise]', 'js_bundle_names': 'List[str]', 'js_model_class': 'Optional[str]', 'js_view_class': 'Optional[str]', 'save_label': 'Optional[StrOrPromise]'}¶
- declared_fields = {'form_target': <django.forms.fields.CharField object>}¶