question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

template filters trip on strings (after errors evaluate to strings)

See original GitHub issue

Under 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_INVALIDthis does not appear to happen.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
akashjobanputracommented, Apr 21, 2019

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 from form.as_p and it worked fine. Thank You.

0reactions
thompsonmanda08commented, Feb 27, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Built-in template tags and filters — Django 4.1.4 documentation
If the variable evaluates to a string, Django will use that string as the name of the parent ... In template.html , the...
Read more >
Django Template Filters - GeeksforGeeks
This filter is used to increment a variable in django Templates. addslashes. It is used to add slashes before quotes. Useful for escaping ......
Read more >
Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >
How to concatenate strings in django templates?
This filter will first try to coerce both values to integers... Strings that can be coerced to integers will be summed, not concatenated....
Read more >
text/template - Go Packages - The Go Programming Language
An argument is a simple value, denoted by one of the following. A boolean, string, character, integer, floating-point, imaginary or complex constant in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found