ilikerobots / cookiecutter-vue-django

Vue 3 + Vite + Django with no compromises. Use Vue SFCs directly in Django Templates, DRF not required.
BSD 3-Clause "New" or "Revised" License
199 stars 22 forks source link

Decimal numbers and localization leads to JavaScript NaN error #91

Open dubst3pp4 opened 4 months ago

dubst3pp4 commented 4 months ago

What happened?

When using Django with a non-English localization, the Decimal values passed from Django to the Vue components are converted to localized values in the Django template, eg. the property points has a value of 2,75 instead of 2.75. This leads to NaN values in JavaScript.

Rendered list.html template:

<div class="reward-claim text-center" data-reward-id="1" data-points="2,75" data-v-app=""><div><a class="btn btn-light">Redeem this Reward</a></div></div>

Possible Solution

Disable localization for Vue components in list.html template:

{% load l10n %}

{% localize off %}
  <div class="reward-claim text-center" data-reward-id="{{ r.id }}" data-points="{{ r.point_value }}"></div>
{% endlocalize %}

Which now renders:

<div class="reward-claim text-center" data-reward-id="1" data-points="2.75" data-v-app=""><div><a class="btn btn-light">Redeem this Reward</a></div></div>

Remarks

Thank you very much for the excellent article and the cookiecutter setup! :+1:

ilikerobots commented 4 months ago

Great catch! Would you like to make a PR?