class ClearableFileInput

from django.forms import ClearableFileInput

Ancestors (MRO)

  1. ClearableFileInput
  2. FileInput
  3. Input
  4. Widget

Attributes

  Defined in
clear_checkbox_label = 'Clear' ClearableFileInput
initial_text = 'Currently' ClearableFileInput
input_text = 'Change' ClearableFileInput
input_type = 'file' FileInput
input_type = None Input
is_localized = False Widget
is_required = False Widget
needs_multipart_form = True FileInput
needs_multipart_form = False Widget
supports_microseconds = True Widget
template_name = 'django/forms/widgets/clearable_file_input.html' ClearableFileInput
template_name = 'django/forms/widgets/file.html' FileInput
template_name = 'django/forms/widgets/input.html' Input
Expand Collapse

Properties

def is_hidden(): Widget

Getter

    @property
    def is_hidden(self):
        return self.input_type == 'hidden' if hasattr(self, 'input_type') else False

def media(): ClearableFileInput

Getter

    def _media(self):
        # Get the media property of the superclass, if it exists
        sup_cls = super(cls, self)
        try:
            base = sup_cls.media
        except AttributeError:
            base = Media()

        # Get the media definition for this class
        definition = getattr(cls, 'Media', None)
        if definition:
            extend = getattr(definition, 'extend', True)
            if extend:
                if extend is True:
                    m = base
                else:
                    m = Media()
                    for medium in extend:
                        m = m + base[medium]
                return m + Media(definition)
            else:
                return Media(definition)
        else:
            return base

def media(): FileInput

Getter

    def _media(self):
        # Get the media property of the superclass, if it exists
        sup_cls = super(cls, self)
        try:
            base = sup_cls.media
        except AttributeError:
            base = Media()

        # Get the media definition for this class
        definition = getattr(cls, 'Media', None)
        if definition:
            extend = getattr(definition, 'extend', True)
            if extend:
                if extend is True:
                    m = base
                else:
                    m = Media()
                    for medium in extend:
                        m = m + base[medium]
                return m + Media(definition)
            else:
                return Media(definition)
        else:
            return base

def media(): Input

Getter

    def _media(self):
        # Get the media property of the superclass, if it exists
        sup_cls = super(cls, self)
        try:
            base = sup_cls.media
        except AttributeError:
            base = Media()

        # Get the media definition for this class
        definition = getattr(cls, 'Media', None)
        if definition:
            extend = getattr(definition, 'extend', True)
            if extend:
                if extend is True:
                    m = base
                else:
                    m = Media()
                    for medium in extend:
                        m = m + base[medium]
                return m + Media(definition)
            else:
                return Media(definition)
        else:
            return base

def media(): Widget

Getter

    def _media(self):
        # Get the media property of the superclass, if it exists
        sup_cls = super(cls, self)
        try:
            base = sup_cls.media
        except AttributeError:
            base = Media()

        # Get the media definition for this class
        definition = getattr(cls, 'Media', None)
        if definition:
            extend = getattr(definition, 'extend', True)
            if extend:
                if extend is True:
                    m = base
                else:
                    m = Media()
                    for medium in extend:
                        m = m + base[medium]
                return m + Media(definition)
            else:
                return Media(definition)
        else:
            return base
Expand Collapse

Methods

def _render(self, template_name, context, renderer=None): Widget

    def _render(self, template_name, context, renderer=None):
        if renderer is None:
            renderer = get_default_renderer()
        return mark_safe(renderer.render(template_name, context))

def build_attrs(self, base_attrs, extra_attrs=None): Widget

Build an attribute dictionary.
    def build_attrs(self, base_attrs, extra_attrs=None):
        """Build an attribute dictionary."""
        attrs = base_attrs.copy()
        if extra_attrs is not None:
            attrs.update(extra_attrs)
        return attrs

def clear_checkbox_id(self, name): ClearableFileInput

        Given the name of the clear checkbox input, return the HTML id for it.
        
    def clear_checkbox_id(self, name):
        """
        Given the name of the clear checkbox input, return the HTML id for it.
        """
        return name + '_id'

def clear_checkbox_name(self, name): ClearableFileInput

        Given the name of the file input, return the name of the clear checkbox
        input.
        
    def clear_checkbox_name(self, name):
        """
        Given the name of the file input, return the name of the clear checkbox
        input.
        """
        return name + '-clear'

def format_value(self, value):

ClearableFileInput

        Return the file object if it has a defined url attribute.
        
    def format_value(self, value):
        """
        Return the file object if it has a defined url attribute.
        """
        if self.is_initial(value):
            return value

FileInput

File input never renders a value.
    def format_value(self, value):
        """File input never renders a value."""
        return

Widget

        Return a value as it should appear when rendered in a template.
        
    def format_value(self, value):
        """
        Return a value as it should appear when rendered in a template.
        """
        if value == '' or value is None:
            return None
        if self.is_localized:
            return formats.localize_input(value)
        return str(value)

def get_context(self, name, value, attrs):

ClearableFileInput

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)
        checkbox_name = self.clear_checkbox_name(name)
        checkbox_id = self.clear_checkbox_id(checkbox_name)
        context['widget'].update({
            'checkbox_name': checkbox_name,
            'checkbox_id': checkbox_id,
            'is_initial': self.is_initial(value),
            'input_text': self.input_text,
            'initial_text': self.initial_text,
            'clear_checkbox_label': self.clear_checkbox_label,
        })
        return context

Input

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)
        context['widget']['type'] = self.input_type
        return context

Widget

    def get_context(self, name, value, attrs):
        context = {}
        context['widget'] = {
            'name': name,
            'is_hidden': self.is_hidden,
            'required': self.is_required,
            'value': self.format_value(value),
            'attrs': self.build_attrs(self.attrs, attrs),
            'template_name': self.template_name,
        }
        return context

def id_for_label(self, id_): Widget

        Return the HTML ID attribute of this Widget for use by a <label>,
        given the ID of the field. Return 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.
        
    def id_for_label(self, id_):
        """
        Return the HTML ID attribute of this Widget for use by a <label>,
        given the ID of the field. Return 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.
        """
        return id_

def is_initial(self, value): ClearableFileInput

        Return whether value is considered to be initial value.
        
    def is_initial(self, value):
        """
        Return whether value is considered to be initial value.
        """
        return bool(value and getattr(value, 'url', False))

def render(self, name, value, attrs=None, renderer=None): Widget

Render the widget as an HTML string.
    def render(self, name, value, attrs=None, renderer=None):
        """Render the widget as an HTML string."""
        context = self.get_context(name, value, attrs)
        return self._render(self.template_name, context, renderer)

def subwidgets(self, name, value, attrs=None): Widget

    def subwidgets(self, name, value, attrs=None):
        context = self.get_context(name, value, attrs)
        yield context['widget']

def use_required_attribute(self, initial):

ClearableFileInput

    def use_required_attribute(self, initial):
        return super().use_required_attribute(initial) and not initial

Widget

    def use_required_attribute(self, initial):
        return not self.is_hidden

def value_from_datadict(self, data, files, name):

ClearableFileInput

    def value_from_datadict(self, data, files, name):
        upload = super().value_from_datadict(data, files, name)
        if not self.is_required and CheckboxInput().value_from_datadict(
                data, files, self.clear_checkbox_name(name)):

            if upload:
                # If the user contradicts themselves (uploads a new file AND
                # checks the "clear" checkbox), we return a unique marker
                # object that FileField will turn into a ValidationError.
                return FILE_INPUT_CONTRADICTION
            # False signals to clear any existing value, as opposed to just None
            return False
        return upload

FileInput

File widgets take data from FILES, not POST
    def value_from_datadict(self, data, files, name):
        "File widgets take data from FILES, not POST"
        return files.get(name)

Widget

        Given a dictionary of data and this widget's name, return the value
        of this widget or None if it's not provided.
        
    def value_from_datadict(self, data, files, name):
        """
        Given a dictionary of data and this widget's name, return the value
        of this widget or None if it's not provided.
        """
        return data.get(name)

def value_omitted_from_data(self, data, files, name):

ClearableFileInput

    def value_omitted_from_data(self, data, files, name):
        return (
            super().value_omitted_from_data(data, files, name) and
            self.clear_checkbox_name(name) not in data
        )

FileInput

    def value_omitted_from_data(self, data, files, name):
        return name not in files

Widget

    def value_omitted_from_data(self, data, files, name):
        return name not in data