template filters trip on strings (after errors evaluate to strings)
See original GitHub issueUnder some conditions the uniform templatetags may obfuscate underlying errors. The conditions are:
1] An invalid left hand side of the filter. E.g.:
{{ willfail|as_crispy_field }}
2] TEMPLATE_STRING_IF_INVALID = ""
(i.e. production environments)
The error that shows up is: `‘str’ object has no attribute ‘field’`` (in is_checkbox, in https://github.com/maraujop/django-crispy-forms/blob/dev/crispy_forms/templatetags/crispy_forms_field.py)
This is because is_checkbox is the first template filter that is used inside as_crispy_field (see: https://github.com/maraujop/django-crispy-forms/blob/dev/crispy_forms/templates/uni_form/field.html).
And under those conditions (an error triggered on the LHS) the filter will still be evaluated, but on the string “”. And it cannot deal with that case. I’m not sure if this should be considered a Django bug rather than crispy-forms, or whether filters are generally expected to be able to handle the case of the empty string. If the former is the case feel free to [instruct me to] file a bug at Django’s tickiting system.
If ‘willfail’ is something non-trivial, i.e. a form field that will fail only under certain specific conditions, this is highly confusing. Firstly because it does not happen in debug environments. Secondly because the actual error is hidden behind crispy-forms tripping on the string.
Further information: for any non-empty value of TEMPLATE_STRING_IF_INVALID
this does not appear to happen.
Issue Analytics
- State:
- Created 11 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top GitHub Comments
This issue is still nagging me, any solutions for it or any guides to look for finding the actual issue?
UPDATE: I removed
.as_p
fromform.as_p
and it worked fine. Thank You.Removing the “.as_p” when rendering the form in my templates worked for me too. I actually did not understand the error and where it was coming from. Even though I am at intermediate level I was sure there was nothing wrong with my code.