django-sniplates icon indicating copy to clipboard operation
django-sniplates copied to clipboard

Attempting to render one of the django.html inputs without backing it with a form field errors

Open kezabelle opened this issue 8 years ago • 3 comments

Given: {% widget "form_alias:TextInput" id="lol" raw_value="lol2" %} An exception is raised: AttributeError: 'str' object has no attribute 'items'

because {{ widget.attrs|flatattrs }} receives '' (or TEMPLATE_STRING_IF_INVALID maybe? dunno) due to not having a Widget instance in the context.

Probably the fix is to wrap the widget attrs output in an IfNode?

kezabelle avatar Feb 22 '17 10:02 kezabelle

If I remember correctly from the Django docs, filters should fail silently if something goes wrong, so then https://github.com/funkybob/django-sniplates/blob/master/sniplates/templatetags/sniplates.py#L497 should be edited to something like:

@register.filter
def flatattrs(attrs):
    if not attrs:
        return TEMPLATE_STRING_IF_INVALID
    return flatatt(attrs)

@funkybob thoughts?

sergei-maertens avatar Feb 22 '17 10:02 sergei-maertens

Actually there's a setting about if a failure should be silent, IIRC.

I think there are some DTL errors that are never swallowed, too.

schinckel avatar Feb 22 '17 20:02 schinckel

I think in this case the filter ought default to safe.

funkybob avatar Feb 22 '17 23:02 funkybob