django-smart-selects icon indicating copy to clipboard operation
django-smart-selects copied to clipboard

bindfields.js expects 'data-value' to be JSON but widgets.py doesn't always encode it

Open davmlaw opened this issue 8 years ago • 2 comments
trafficstars

Checklist

Put an x in the bracket when you have completed each task, like this: [x]

  • [ X] This issue is not about installing previous versions of django-smart-selects older than 1.2.8. I understand that previous versions are insecure and will not receive any support whatsoever.
  • [ X] I have verified that that issue exists against the master branch of django-smart-selects.
  • [ X] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [ X] I have debugged the issue to the smart_selects app.
  • [ X] I have reduced the issue to the simplest possible case.
  • [ X] I have included all relevant sections of models.py, forms.py, and views.py with problems.
  • [X ] I have used GitHub Flavored Markdown to style all of my posted code.

Steps to reproduce

  1. Have a formset with 2 smart selects, load the page

Actual behavior

Uncaught SyntaxError: Unexpected token _ in JSON at position 2 at JSON.parse (<anonymous>) at initItem (bindfields.js:8) at HTMLSelectElement.<anonymous> (bindfields.js:25) at Function.each (jquery-2.2.4.js:365) at HTMLDocument.<anonymous> (bindfields.js:24) at fire (jquery-2.2.4.js:3187) at Object.fireWith [as resolveWith] (jquery-2.2.4.js:3317) at Function.ready (jquery-2.2.4.js:3536) at HTMLDocument.completed (jquery-2.2.4.js:3552)

Expected behavior

No Javascript error occurs.

Suggested Fix:

The problem is that in widgets.py, ChainedSelect.render line 138 sets:

attrs["data-value"] = "null" if value is None or value == "" else value

So when the string "foo" is decoded, it fails with JSON decode error above (should be '"foo"' (with quotes))

I believe the fix should be:

attrs["data-value"] = "null" if value is None else json.dumps(value)

Which is the same as line 244 in ChainedSelectMultiple

davmlaw avatar Jul 20 '17 06:07 davmlaw

@davmlaw can you reproduce with latest master version?

manelclos avatar Apr 26 '20 15:04 manelclos

Sorry, I use Django autocomplete light now, so can't easily test this. Feel free to close if you think it's fixed.

davmlaw avatar May 07 '20 00:05 davmlaw