Exemplo n.º 1
0
def as_crispy_field(
    field,
    template_pack=TEMPLATE_PACK,
    label_class="",
    field_class="",
    wrapper_class="",
    show_labels=True,
):
    # inspired by original as_crispy_field, but it is not callable as filter and does not support wrapper class/labels
    from django.conf import settings

    if not isinstance(field, boundfield.BoundField) and settings.DEBUG:
        raise CrispyError(
            "|as_crispy_field got passed an invalid or inexistent field")

    attributes = {
        "field": field,
        "form_show_errors": True,
        "form_show_labels": show_labels,
        "label_class": label_class,
        "field_class": field_class,
        "wrapper_class": wrapper_class,
    }
    helper = getattr(field.form, "helper", None)

    template_path = None
    if helper is not None:
        attributes.update(helper.get_attributes(template_pack))
        template_path = helper.field_template
    if not template_path:
        template_path = "%s/field.html" % template_pack
    template = get_template(template_path)

    c = Context(attributes).flatten()
    return template.render(c)
Exemplo n.º 2
0
def as_crispy_field(field, template_pack=TEMPLATE_PACK, label_class="", field_class=""):
    """
    Renders a form field like a django-crispy-forms field::

        {% load crispy_forms_tags %}
        {{ form.field|as_crispy_field }}

    or::

        {{ form.field|as_crispy_field:"css" }}
    """
    if not isinstance(field, boundfield.BoundField) and settings.DEBUG:
        raise CrispyError("|as_crispy_field got passed an invalid or inexistent field")

    attributes = {
        "field": field,
        "form_show_errors": True,
        "form_show_labels": True,
        "label_class": label_class,
        "field_class": field_class,
    }
    helper = getattr(field.form, "helper", None)

    template_path = None
    if helper is not None:
        attributes.update(helper.get_attributes(template_pack))
        template_path = helper.field_template
    if not template_path:
        template_path = "%s/field.html" % template_pack
    template = get_template(template_path)

    c = Context(attributes).flatten()
    return template.render(c)
def as_crispy_field(field, template_pack=TEMPLATE_PACK, label_class="", field_class=""):
    """
    Renders a form field like a django-crispy-forms field::

        {% load crispy_forms_tags %}
        {{ form.field|as_crispy_field }}

    or::

        {{ form.field|as_crispy_field:"bootstrap" }}
    """
    if not isinstance(field, forms.BoundField) and settings.DEBUG:
        raise CrispyError('|as_crispy_field got passed an invalid or inexistent field')

    attributes = {
        'field': field,
        'form_show_errors': True,
        'form_show_labels': True,
        'label_class': label_class,
        'field_class': field_class,
    }
    helper = getattr(field.form, 'helper', None)

    template_path = None
    if helper is not None:
        attributes.update(helper.get_attributes(template_pack))
        template_path = helper.field_template
    if not template_path:
        template_path = '%s/field.html' % template_pack
    template = get_template(template_path)

    c = Context(attributes).flatten()
    return template.render(c)
def as_crispy_field(field, template_pack=TEMPLATE_PACK):
    """
    Renders a form field like a django-crispy-forms field::

        {% load crispy_forms_tags %}
        {{ form.field|as_crispy_field }}

    or::

        {{ form.field|as_crispy_field:"bootstrap" }}
    """
    if not isinstance(field, forms.BoundField) and settings.DEBUG:
        raise CrispyError(
            '|as_crispy_field got passed an invalid or inexistent field')

    template = get_template('%s/field.html' % template_pack)
    c = Context({
        'field': field,
        'form_show_errors': True,
        'form_show_labels': True
    })

    if django.VERSION >= (1, 8):
        c = c.flatten()

    return template.render(c)
def bs4_appended_prepended_text(field,
                                append="",
                                prepend="",
                                form_show_labels=True):
    """
    Similar to the `crispy_addon` tag. However, this one respects classes that
    have been set in the corresponding Form layout object.
    """

    template_pack = get_template_pack()
    if template_pack != "bootstrap4":
        raise CrispyError(
            "bs4_appended_prepended_text can only be used with Bootstrap 4")

    if field:
        attributes = {
            'field': field,
            'form_show_errors': True,
            'form_show_labels': form_show_labels,
        }
        helper = getattr(field.form, "helper", None)
        if helper is not None:
            attributes.update(helper.get_attributes(get_template_pack()))

        context = Context(attributes)
        template = loader.get_template(
            "%s/layout/prepended_appended_text.html" % get_template_pack())
        context["crispy_prepended_text"] = prepend
        context["crispy_appended_text"] = append

        if not prepend and not append:
            raise TypeError("Expected a prepend and/or append argument")

        context = context.flatten()

    return template.render(context)