Skip to content

widgets

django_spire.file.widgets

MultipleFileWidget

Bases: Widget

needs_multipart_form = True class-attribute instance-attribute

template_name = 'django_spire/file/widget/multiple_file_widget.html' class-attribute instance-attribute

render

Source code in django_spire/file/widgets.py
def render(
    self,
    name: str,
    value: str | None,
    attrs: dict | None = None,
    _renderer: object | None = None,
) -> SafeString:
    context = self.get_context(name, value, attrs)
    template = loader.get_template(self.template_name).render(context)
    return mark_safe(template)  # noqa: S308

value_from_datadict

Source code in django_spire/file/widgets.py
def value_from_datadict(self, data: dict, files: dict, name: str) -> list[dict]:
    json_data = data.get(f'{name}_data')

    if json_data is not None:
        try:
            return json.loads(json_data)
        except (json.JSONDecodeError, TypeError):
            return []

    if files:
        return files.getlist(name)

    return []

SingleFileWidget

Bases: Widget

needs_multipart_form = True class-attribute instance-attribute

template_name = 'django_spire/file/widget/single_file_widget.html' class-attribute instance-attribute

render

Source code in django_spire/file/widgets.py
def render(
    self,
    name: str,
    value: str | None,
    attrs: dict | None = None,
    _renderer: object | None = None,
) -> SafeString:
    context = self.get_context(name, value, attrs)
    template = loader.get_template(self.template_name).render(context)
    return mark_safe(template)  # noqa: S308

value_from_datadict

Source code in django_spire/file/widgets.py
def value_from_datadict(self, data: dict, files: dict, name: str) -> dict | None:
    json_data = data.get(f'{name}_data')

    if json_data is not None:
        try:
            return json.loads(json_data)
        except (json.JSONDecodeError, TypeError):
            return None

    if files and name in files:
        return files[name]

    return None