The horizon.forms Module

class horizon.forms.SelfHandlingMixin(request, *args, **kwargs)

Bases: object

class horizon.forms.SelfHandlingForm(request, *args, **kwargs)

Bases: horizon.forms.base.SelfHandlingMixin, django.forms.forms.Form

A base Form class which includes processing logic in its subclasses.

api_error(message)

Adds an error to the form’s error dictionary after validation based on problems reported via the API. This is useful when you wish for API errors to appear as errors on the form rather than using the messages framework.

base_fields = {}
media
required_css_class = 'required'
set_warning(message)

Sets a warning on the form.

Unlike NON_FIELD_ERRORS, this doesn’t fail form validation.

class horizon.forms.DateForm(*args, **kwargs)

Bases: django.forms.forms.Form

A simple form for selecting a range of time.

base_fields = {'start': <django.forms.fields.DateField object at 0x2b77f3264c50>, 'end': <django.forms.fields.DateField object at 0x2b77f45503d0>}
media
class horizon.forms.ModalFormView

Bases: horizon.forms.views.ModalBackdropMixin, horizon.forms.views.ModalFormMixin, horizon.views.HorizonFormView

The main view class from which all views which handle forms in Horizon should inherit. It takes care of all details with processing SelfHandlingForm classes, and modal concerns when the associated template inherits from horizon/common/_modal_form.html.

Subclasses must define a form_class and template_name attribute at minimum.

See Django’s documentation on the FormView class for more details.

cancel_label = <django.utils.functional.__proxy__ object at 0x2b77f44922d0>
cancel_url = None
form_id = None
form_valid(form)
get_cancel_url()
get_context_data(**kwargs)
get_form(form_class)

Returns an instance of the form to be used in this view.

get_object_display(obj)

For dynamic insertion of resources created in modals, this method returns the display name of the created object. Defaults to returning the name attribute.

get_object_id(obj)

For dynamic insertion of resources created in modals, this method returns the id of the created object. Defaults to returning the id attribute.

modal_header = ''
modal_id = None
submit_label = <django.utils.functional.__proxy__ object at 0x2b77f44a3b50>
submit_url = None
class horizon.forms.ModalFormMixin

Bases: object

get_context_data(**kwargs)
get_template_names()
class horizon.forms.DynamicTypedChoiceField(add_item_link=None, add_item_link_args=None, *args, **kwargs)

Bases: horizon.forms.fields.DynamicChoiceField, django.forms.fields.TypedChoiceField

Simple mix of DynamicChoiceField and TypedChoiceField.

class horizon.forms.DynamicChoiceField(add_item_link=None, add_item_link_args=None, *args, **kwargs)

Bases: django.forms.fields.ChoiceField

A subclass of ChoiceField with additional properties that make dynamically updating its elements easier.

Notably, the field declaration takes an extra argument, add_item_link which may be a string or callable defining the URL that should be used for the “add” link associated with the field.

widget

alias of DynamicSelectWidget

class horizon.forms.IPField(*args, **kwargs)

Bases: django.forms.fields.Field

Form field for entering IP/range values, with validation. Supports IPv4/IPv6 in the format: .. xxx.xxx.xxx.xxx .. xxx.xxx.xxx.xxx/zz .. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff .. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/zz and all compressed forms. Also the short forms are supported: xxx/yy xxx.xxx/yy

version

Specifies which IP version to validate, valid values are 1 (fields.IPv4), 2 (fields.IPv6) or both - 3 (fields.IPv4 | fields.IPv6). Defaults to IPv4 (1)

mask

Boolean flag to validate subnet masks along with IP address. E.g: 10.0.0.1/32

mask_range_from
Subnet range limitation, e.g. 16
That means the input mask will be checked to be in the range
16:max_value. Useful to limit the subnet ranges
to A/B/C-class networks.
clean(value)
invalid_format_message = <django.utils.functional.__proxy__ object at 0x2b77f44c0210>
invalid_mask_message = <django.utils.functional.__proxy__ object at 0x2b77f44c0310>
invalid_version_message = <django.utils.functional.__proxy__ object at 0x2b77f44c0290>
max_v4_mask = 32
max_v6_mask = 128
validate(value)
class horizon.forms.MultiIPField(*args, **kwargs)

Bases: horizon.forms.fields.IPField

Extends IPField to allow comma-separated lists of addresses.

clean(value)
validate(value)
class horizon.forms.Field(required=True, widget=None, label=None, initial=None, help_text=u'', error_messages=None, show_hidden_initial=False, validators=, []localize=False)

Bases: object

bound_data(data, initial)

Return the value that should be shown for this field on render of a bound form, given the submitted POST data for the field and the initial data, if any.

For most fields, this will simply be data; FileFields need to handle it a bit differently.

clean(value)

Validates the given value and returns its “cleaned” value as an appropriate Python object.

Raises ValidationError for any errors.

creation_counter = 31
default_error_messages = {u'required': <django.utils.functional.__proxy__ object at 0x2b77f410e1d0>}
default_validators = []
empty_values = [None, u'', [], (), {}]
hidden_widget

alias of HiddenInput

prepare_value(value)
run_validators(value)
to_python(value)
validate(value)
widget

alias of TextInput

widget_attrs(widget)

Given a Widget instance (not a Widget class), returns a dictionary of any HTML attributes that should be added to the Widget, based on this Field.

class horizon.forms.CharField(max_length=None, min_length=None, *args, **kwargs)

Bases: django.forms.fields.Field

to_python(value)

Returns a Unicode object.

widget_attrs(widget)
class horizon.forms.IntegerField(max_value=None, min_value=None, *args, **kwargs)

Bases: django.forms.fields.Field

default_error_messages = {u'invalid': <django.utils.functional.__proxy__ object at 0x2b77f410e310>}
to_python(value)

Validates that int() can be called on the input. Returns the result of int(). Returns None for empty values.

widget_attrs(widget)
class horizon.forms.DateField(input_formats=None, *args, **kwargs)

Bases: django.forms.fields.BaseTemporalField

default_error_messages = {u'invalid': <django.utils.functional.__proxy__ object at 0x2b77f413c690>}
input_formats = <django.utils.functional.__proxy__ object at 0x2b77f413c650>
strptime(value, format)
to_python(value)

Validates that the input can be converted to a date. Returns a Python datetime.date object.

widget

alias of DateInput

class horizon.forms.TimeField(input_formats=None, *args, **kwargs)

Bases: django.forms.fields.BaseTemporalField

default_error_messages = {u'invalid': <django.utils.functional.__proxy__ object at 0x2b77f413c810>}
input_formats = <django.utils.functional.__proxy__ object at 0x2b77f413c790>
strptime(value, format)
to_python(value)

Validates that the input can be converted to a time. Returns a Python datetime.time object.

widget

alias of TimeInput

class horizon.forms.DateTimeField(input_formats=None, *args, **kwargs)

Bases: django.forms.fields.BaseTemporalField

default_error_messages = {u'invalid': <django.utils.functional.__proxy__ object at 0x2b77f413c950>}
input_formats = <django.utils.functional.__proxy__ object at 0x2b77f413c8d0>
prepare_value(value)
strptime(value, format)
to_python(value)

Validates that the input can be converted to a datetime. Returns a Python datetime.datetime object.

widget

alias of DateTimeInput

class horizon.forms.TimeField(input_formats=None, *args, **kwargs)

Bases: django.forms.fields.BaseTemporalField

default_error_messages = {u'invalid': <django.utils.functional.__proxy__ object at 0x2b77f413c810>}
input_formats = <django.utils.functional.__proxy__ object at 0x2b77f413c790>
strptime(value, format)
to_python(value)

Validates that the input can be converted to a time. Returns a Python datetime.time object.

widget

alias of TimeInput

class horizon.forms.RegexField(regex, max_length=None, min_length=None, error_message=None, *args, **kwargs)

Bases: django.forms.fields.CharField

regex
class horizon.forms.EmailField(max_length=None, min_length=None, *args, **kwargs)

Bases: django.forms.fields.CharField

clean(value)
default_validators = [<django.core.validators.EmailValidator object at 0x2b77f4050350>]
widget

alias of EmailInput

class horizon.forms.FileField(*args, **kwargs)

Bases: django.forms.fields.Field

bound_data(data, initial)
clean(data, initial=None)
default_error_messages = {u'contradiction': <django.utils.functional.__proxy__ object at 0x2b77f413cfd0>, u'max_length': <django.utils.functional.__proxy__ object at 0x2b77f413ce90>, u'empty': <django.utils.functional.__proxy__ object at 0x2b77f413cb90>, u'invalid': <django.utils.functional.__proxy__ object at 0x2b77f413ca90>, u'missing': <django.utils.functional.__proxy__ object at 0x2b77f413cb10>}
to_python(data)
widget

alias of ClearableFileInput

class horizon.forms.ImageField(*args, **kwargs)

Bases: django.forms.fields.FileField

default_error_messages = {u'invalid_image': <django.utils.functional.__proxy__ object at 0x2b77f414e090>}
to_python(data)

Checks that the file-upload field data contains a valid image (GIF, JPG, PNG, possibly others – whatever the Python Imaging Library supports).

class horizon.forms.URLField(max_length=None, min_length=None, *args, **kwargs)

Bases: django.forms.fields.CharField

clean(value)
default_error_messages = {u'invalid': <django.utils.functional.__proxy__ object at 0x2b77f414e150>}
default_validators = [<django.core.validators.URLValidator object at 0x2b77f414e190>]
to_python(value)
widget

alias of URLInput

class horizon.forms.BooleanField(required=True, widget=None, label=None, initial=None, help_text=u'', error_messages=None, show_hidden_initial=False, validators=, []localize=False)

Bases: django.forms.fields.Field

to_python(value)

Returns a Python boolean object.

validate(value)
widget

alias of CheckboxInput

class horizon.forms.NullBooleanField(required=True, widget=None, label=None, initial=None, help_text=u'', error_messages=None, show_hidden_initial=False, validators=, []localize=False)

Bases: django.forms.fields.BooleanField

A field whose valid values are None, True and False. Invalid values are cleaned to None.

to_python(value)

Explicitly checks for the string ‘True’ and ‘False’, which is what a hidden field will submit for True and False, and for ‘1’ and ‘0’, which is what a RadioField will submit. Unlike the Booleanfield we need to explicitly check for True, because we are not using the bool() function

validate(value)
widget

alias of NullBooleanSelect

class horizon.forms.ChoiceField(choices=(), required=True, widget=None, label=None, initial=None, help_text=u'', *args, **kwargs)

Bases: django.forms.fields.Field

choices
default_error_messages = {u'invalid_choice': <django.utils.functional.__proxy__ object at 0x2b77f414e2d0>}
to_python(value)

Returns a Unicode object.

valid_value(value)

Check to see if the provided value is a valid choice

validate(value)

Validates that the input is in self.choices.

widget

alias of Select

class horizon.forms.MultipleChoiceField(choices=(), required=True, widget=None, label=None, initial=None, help_text=u'', *args, **kwargs)

Bases: django.forms.fields.ChoiceField

default_error_messages = {u'invalid_list': <django.utils.functional.__proxy__ object at 0x2b77f414e450>, u'invalid_choice': <django.utils.functional.__proxy__ object at 0x2b77f414e3d0>}
hidden_widget

alias of MultipleHiddenInput

to_python(value)
validate(value)

Validates that the input is a list or tuple.

widget

alias of SelectMultiple

class horizon.forms.ComboField(fields=(), *args, **kwargs)

Bases: django.forms.fields.Field

A Field whose clean() method calls multiple Field clean() methods.

clean(value)

Validates the given value against all of self.fields, which is a list of Field instances.

class horizon.forms.MultiValueField(fields=(), *args, **kwargs)

Bases: django.forms.fields.Field

A Field that aggregates the logic of multiple Fields.

Its clean() method takes a “decompressed” list of values, which are then cleaned into a single value according to self.fields. Each value in this list is cleaned by the corresponding field – the first value is cleaned by the first field, the second value is cleaned by the second field, etc. Once all fields are cleaned, the list of clean values is “compressed” into a single value.

Subclasses should not have to implement clean(). Instead, they must implement compress(), which takes a list of valid values and returns a “compressed” version of those values – a single value.

You’ll probably want to use this with MultiWidget.

clean(value)

Validates every value in the given list. A value is validated against the corresponding Field in self.fields.

For example, if this MultiValueField was instantiated with fields=(DateField(), TimeField()), clean() would call DateField.clean(value[0]) and TimeField.clean(value[1]).

compress(data_list)

Returns a single value for the given list of values. The values can be assumed to be valid.

For example, if this MultiValueField was instantiated with fields=(DateField(), TimeField()), this might return a datetime object created by combining the date and time in data_list.

default_error_messages = {u'invalid': <django.utils.functional.__proxy__ object at 0x2b77f414e5d0>}
validate(value)
class horizon.forms.FloatField(max_value=None, min_value=None, *args, **kwargs)

Bases: django.forms.fields.IntegerField

default_error_messages = {u'invalid': <django.utils.functional.__proxy__ object at 0x2b77f410e450>}
to_python(value)

Validates that float() can be called on the input. Returns the result of float(). Returns None for empty values.

widget_attrs(widget)
class horizon.forms.DecimalField(max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs)

Bases: django.forms.fields.IntegerField

default_error_messages = {u'max_whole_digits': <django.utils.functional.__proxy__ object at 0x2b77f413c4d0>, u'max_digits': <django.utils.functional.__proxy__ object at 0x2b77f412ce10>, u'invalid': <django.utils.functional.__proxy__ object at 0x2b77f410e710>, u'max_decimal_places': <django.utils.functional.__proxy__ object at 0x2b77f413c190>}
to_python(value)

Validates that the input is a decimal number. Returns a Decimal instance. Returns None for empty values. Ensures that there are no more than max_digits in the number, and no more than decimal_places digits after the decimal point.

validate(value)
widget_attrs(widget)
class horizon.forms.SplitDateTimeField(input_date_formats=None, input_time_formats=None, *args, **kwargs)

Bases: django.forms.fields.MultiValueField

compress(data_list)
default_error_messages = {u'invalid_time': <django.utils.functional.__proxy__ object at 0x2b77f414e790>, u'invalid_date': <django.utils.functional.__proxy__ object at 0x2b77f414e710>}
hidden_widget

alias of SplitHiddenDateTimeWidget

widget

alias of SplitDateTimeWidget

class horizon.forms.IPAddressField(max_length=None, min_length=None, *args, **kwargs)

Bases: django.forms.fields.CharField

default_validators = [<django.core.validators.RegexValidator object at 0x2b77f4050490>]
to_python(value)
class horizon.forms.GenericIPAddressField(protocol=u'both', unpack_ipv4=False, *args, **kwargs)

Bases: django.forms.fields.CharField

to_python(value)
class horizon.forms.FilePathField(path, match=None, recursive=False, allow_files=True, allow_folders=False, required=True, widget=None, label=None, initial=None, help_text=u'', *args, **kwargs)

Bases: django.forms.fields.ChoiceField

class horizon.forms.SlugField(max_length=None, min_length=None, *args, **kwargs)

Bases: django.forms.fields.CharField

clean(value)
default_validators = [<django.core.validators.RegexValidator object at 0x2b77f40503d0>]
class horizon.forms.TypedChoiceField(*args, **kwargs)

Bases: django.forms.fields.ChoiceField

to_python(value)

Validates that the value is in self.choices and can be coerced to the right type.

class horizon.forms.TypedMultipleChoiceField(*args, **kwargs)

Bases: django.forms.fields.MultipleChoiceField

to_python(value)

Validates that the values are in self.choices and can be coerced to the right type.

validate(value)
class horizon.forms.Media(media=None, **kwargs)

Bases: object

absolute_path(path, prefix=None)
add_css(data)
add_js(data)
render()
render_css()
render_js()
class horizon.forms.MediaDefiningClass

Bases: type

Metaclass for classes that can have media definitions

class horizon.forms.Widget(attrs=None)

Bases: django.forms.widgets.NewBase

build_attrs(extra_attrs=None, **kwargs)

Helper function for building an attribute dictionary.

id_for_label(id_)

Returns the HTML ID attribute of this Widget for use by a <label>, given the ID of the field. Returns None if no ID is available.

This hook is necessary because some widgets have multiple HTML elements and, thus, multiple IDs. In that case, this method should return an ID value that corresponds to the first ID in the widget’s tags.

is_hidden = False
is_localized = False
is_required = False
media
needs_multipart_form = False
render(name, value, attrs=None)

Returns this Widget rendered as HTML, as a Unicode string.

The ‘value’ given is not guaranteed to be valid input, so subclass implementations should program defensively.

subwidgets(name, value, attrs=None, choices=())

Yields all “subwidgets” of this widget. Used only by RadioSelect to allow template access to individual <input type=”radio”> buttons.

Arguments are the same as for render().

value_from_datadict(data, files, name)

Given a dictionary of data and this widget’s name, returns the value of this widget. Returns None if it’s not provided.

class horizon.forms.TextInput(attrs=None)

Bases: django.forms.widgets.Input

input_type = u'text'
media
class horizon.forms.PasswordInput(attrs=None, render_value=False)

Bases: django.forms.widgets.TextInput

input_type = u'password'
media
render(name, value, attrs=None)
class horizon.forms.HiddenInput(attrs=None)

Bases: django.forms.widgets.Input

input_type = u'hidden'
is_hidden = True
media
class horizon.forms.MultipleHiddenInput(attrs=None, choices=())

Bases: django.forms.widgets.HiddenInput

A widget that handles <input type=”hidden”> for fields that have a list of values.

media
render(name, value, attrs=None, choices=())
value_from_datadict(data, files, name)
class horizon.forms.ClearableFileInput(attrs=None)

Bases: django.forms.widgets.FileInput

clear_checkbox_id(name)

Given the name of the clear checkbox input, return the HTML id for it.

clear_checkbox_label = <django.utils.functional.__proxy__ object at 0x2b77f412c390>
clear_checkbox_name(name)

Given the name of the file input, return the name of the clear checkbox input.

initial_text = <django.utils.functional.__proxy__ object at 0x2b77f412c290>
input_text = <django.utils.functional.__proxy__ object at 0x2b77f412c310>
media
render(name, value, attrs=None)
template_with_clear = u'%(clear)s <label for="%(clear_checkbox_id)s">%(clear_checkbox_label)s</label>'
template_with_initial = u'%(initial_text)s: %(initial)s %(clear_template)s<br />%(input_text)s: %(input)s'
url_markup_template = u'<a href="{0}">{1}</a>'
value_from_datadict(data, files, name)
class horizon.forms.FileInput(attrs=None)

Bases: django.forms.widgets.Input

input_type = u'file'
media
needs_multipart_form = True
render(name, value, attrs=None)
value_from_datadict(data, files, name)

File widgets take data from FILES, not POST

class horizon.forms.DateInput(attrs=None, format=None)

Bases: django.forms.widgets.TextInput

media
class horizon.forms.DateTimeInput(attrs=None, format=None)

Bases: django.forms.widgets.TextInput

media
class horizon.forms.TimeInput(attrs=None, format=None)

Bases: django.forms.widgets.TextInput

media
class horizon.forms.Textarea(attrs=None)

Bases: django.forms.widgets.Widget

media
render(name, value, attrs=None)
class horizon.forms.CheckboxInput(attrs=None, check_test=None)

Bases: django.forms.widgets.Widget

media
render(name, value, attrs=None)
value_from_datadict(data, files, name)
class horizon.forms.Select(attrs=None, choices=())

Bases: django.forms.widgets.Widget

allow_multiple_selected = False
media
render(name, value, attrs=None, choices=())
render_option(selected_choices, option_value, option_label)
render_options(choices, selected_choices)
class horizon.forms.NullBooleanSelect(attrs=None)

Bases: django.forms.widgets.Select

A Select Widget intended to be used with NullBooleanField.

media
render(name, value, attrs=None, choices=())
value_from_datadict(data, files, name)
class horizon.forms.SelectMultiple(attrs=None, choices=())

Bases: django.forms.widgets.Select

allow_multiple_selected = True
media
render(name, value, attrs=None, choices=())
value_from_datadict(data, files, name)
class horizon.forms.RadioSelect(*args, **kwargs)

Bases: django.forms.widgets.RendererMixin, django.forms.widgets.Select

media
renderer

alias of RadioFieldRenderer

class horizon.forms.CheckboxSelectMultiple(*args, **kwargs)

Bases: django.forms.widgets.RendererMixin, django.forms.widgets.SelectMultiple

media
renderer

alias of CheckboxFieldRenderer

class horizon.forms.MultiWidget(widgets, attrs=None)

Bases: django.forms.widgets.Widget

A widget that is composed of multiple widgets.

Its render() method is different than other widgets’, because it has to figure out how to split a single value for display in multiple widgets. The value argument can be one of two things:

  • A list.
  • A normal value (e.g., a string) that has been “compressed” from a list of values.

In the second case – i.e., if the value is NOT a list – render() will first “decompress” the value into a list before rendering it. It does so by calling the decompress() method, which MultiWidget subclasses must implement. This method takes a single “compressed” value and returns a list.

When render() does its HTML rendering, each value in the list is rendered with the corresponding widget – the first value is rendered in the first widget, the second value is rendered in the second widget, etc.

Subclasses may implement format_output(), which takes the list of rendered widgets and returns a string of HTML that formats them any way you’d like.

You’ll probably want to use this class with MultiValueField.

decompress(value)

Returns a list of decompressed values for the given compressed value. The given value can be assumed to be valid, but not necessarily non-empty.

format_output(rendered_widgets)

Given a list of rendered widgets (as strings), returns a Unicode string representing the HTML for the whole lot.

This hook allows you to format the HTML design of the widgets, if needed.

id_for_label(id_)
media

Media for a multiwidget is the combination of all media of the subwidgets

needs_multipart_form
render(name, value, attrs=None)
value_from_datadict(data, files, name)
class horizon.forms.SplitDateTimeWidget(attrs=None, date_format=None, time_format=None)

Bases: django.forms.widgets.MultiWidget

A Widget that splits datetime input into two <input type=”text”> boxes.

decompress(value)
media
class horizon.forms.BaseForm(data=None, files=None, auto_id=u'id_%s', prefix=None, initial=None, error_class=<class 'django.forms.util.ErrorList'>, label_suffix=None, empty_permitted=False)

Bases: object

add_initial_prefix(field_name)

Add a ‘initial’ prefix for checking dynamic initial values

add_prefix(field_name)

Returns the field name with a prefix appended, if this Form has a prefix set.

Subclasses may wish to override.

as_p()

Returns this form rendered as HTML <p>s.

as_table()

Returns this form rendered as HTML <tr>s – excluding the <table></table>.

as_ul()

Returns this form rendered as HTML <li>s – excluding the <ul></ul>.

changed_data
clean()

Hook for doing any extra form-wide cleaning after Field.clean() been called on every field. Any ValidationError raised by this method will not be associated with a particular field; it will have a special-case association with the field named ‘__all__’.

errors

Returns an ErrorDict for the data provided for the form

full_clean()

Cleans all of self.data and populates self._errors and self.cleaned_data.

has_changed()

Returns True if data differs from initial.

hidden_fields()

Returns a list of all the BoundField objects that are hidden fields. Useful for manual form layout in templates.

is_multipart()

Returns True if the form needs to be multipart-encoded, i.e. it has FileInput. Otherwise, False.

is_valid()

Returns True if the form has no errors. Otherwise, False. If errors are being ignored, returns False.

media

Provide a description of all media required to render the widgets on this form

non_field_errors()

Returns an ErrorList of errors that aren’t associated with a particular field – i.e., from Form.clean(). Returns an empty ErrorList if there are none.

visible_fields()

Returns a list of BoundField objects that aren’t hidden fields. The opposite of the hidden_fields() method.

class horizon.forms.Form(data=None, files=None, auto_id=u'id_%s', prefix=None, initial=None, error_class=<class 'django.forms.util.ErrorList'>, label_suffix=None, empty_permitted=False)

Bases: django.forms.forms.NewBase

A collection of Fields, plus their associated data.

base_fields = {}
media

Previous topic

The horizon.forms.views Module

Next topic

The horizon.forms.base Module

This Page