29👍
You can now force the value to be printed without localization.
{% load l10n %}
{{ value|unlocalize }}
Taken from https://docs.djangoproject.com/en/2.2/topics/i18n/formatting/#std:templatefilter-unlocalize
- [Django]-Django REST Framework: how to substitute null with empty string?
- [Django]-What are the differences between setUpClass, setUpTestData and setUp in TestCase class?
- [Django]-ReactJS with Django – real usage
- [Django]-Get the latest record with filter in Django
- [Django]-Optional fields in django models
- [Django]-Django: How to check if the user left all fields blank (or to initial values)?
5👍
You need to create a custom template filter.
from django.template import Library
from django.utils.numberformat import format
register = Library()
@register.filter
def floatdot(value, decimal_pos=4):
return format(value, ".", decimal_pos)
floatdot.is_safe = True
Usage:
{{ float_var|floatdot }}
or {{ float_var|floatdot:2 }}
- [Django]-Is there a way to undo a migration on Django and uncheck it from the list of showmigrations?
- [Django]-Variable subtraction in django templates
- [Django]-How to pass kwargs from save to post_save signal
2👍
You could use a custom formats.py
(see “Creating custom format files” in the Django docs) and define THOUSAND_SEPARATOR
and DECIMAL_SEPARATOR
THOUSAND_SEPARATOR = ''
DECIMAL_SEPARATOR = '.'
This is a global setting, so it will affect all floats displayed on your site. And you’ll have to turn on localization (USE_L10N
in your settings.py).
If you have control over the template, you could simply remove the floatformat
filter.
edit: I’m not sure, but perhaps you are a victim of this Django bug: #13617. Try to turn off localization support in your settings.py and see if the erroneous commas disappear:
USE_L10N = False
If that is the case, have a look at the various workarounds mentioned in the bugreport (the simplest being to turn localization off if you don’t need it anyway).
- [Django]-Going from twitter date to Python datetime date
- [Django]-How to debug gunicorn failure issues? (Worker failed to boot)
- [Django]-Django catch-all URL without breaking APPEND_SLASH
2👍
I’ve got the same issue, and as piquadrat says, it’s an annoying bug related to localization support. Changing USE_L10N = True
to False
solve this, it is suppposed to be fix in Django 1.3.
- [Django]-Workflow frameworks for Django
- [Django]-Running a Python script outside of Django
- [Django]-Is there a way to filter a queryset in the django admin?
0👍
When print some variable for javascrip, its better to jsonify it. Write a jsonify template tag then use
{{value|jsonify}}
Template tags
from django.core.serializers import serialize
from django.db.models.query import QuerySet
import json
from django.template import Library
register = Library()
def jsonify(object):
if isinstance(object, QuerySet):
return serialize('json', object)
return json.dumps(object)
register.filter('jsonify', jsonify)
- [Django]-Django: Is there a way to keep the dev server from restarting when a local .py file is changed and dynamically loaded?
- [Django]-Mongoengine creation_time attribute in Document
- [Django]-Populating django field with pre_save()?