-1

I have a Decimal field in form.py like this: `

quantity = forms.DecimalField(required=True, widget=forms.NumberInput(
    attrs={'class': 'form-control item-quantity', 'style': 'text-align: right'}),
    error_messages={'required': 'This field is required.'}, localize=True) 

In html I have that: `

{{ formset_item.management_form }}
{% for form in formset_item.forms %}
 <tr class="gradeX">
     <td>{{ form.quantity }}</td>
 </tr>
{% endfor %}

Now I want to input number in this quantity input. ex: When I enter input (look like key press event) 1000000 into this input intext it will auto format to 1,000,000 for user see not insert to database

trent fernandez
  • 329
  • 4
  • 19
  • See [Locale aware input in forms](https://docs.djangoproject.com/en/1.9/topics/i18n/formatting/#locale-aware-input-in-forms) – solarissmoke Jul 06 '16 at 06:17

3 Answers3

0
total=100000
print ("Total cost is: ${:,.2f}".format(total)

first of all you convert your o/p in string then after store in database try it....

lol.. sorry for my bad english :)

Dipnesh Parakhiya
  • 516
  • 1
  • 5
  • 17
0

I'm not sure if I understand your requirements correctly, but I think I have the gist of it:

  1. User can enter data in a form (numerical) which will be saved to the database as an integer
  2. When the data is next presented to the user (for editing?) it will be formatted with commas (1,000 | 100,000 | 87,654,321) which then can be edited?
  3. You may or may not want the system to convert entered numbers to comma-separated on the fly

For number 1 & 2, when are you pulling from and updating the database, you can simply convert the numbers to and from strings when needed with helper functions: How to print number with commas as thousands separators? How do I use Python to convert a string to a number if it has commas in it as thousands separators?

If you want it to happen in realtime, you will need Javascript. I would probably just have an onfocus listener that would convert the comma based number to a regular int form, then onblur it would convert it back to the comma formatted number (You would probably need some additional listeners to handle edge cases?).

Edit: If you want a more simple method, I am not familiar with it, but it seems this may be of help to you Format numbers in django templates

Community
  • 1
  • 1
Psyrus
  • 511
  • 4
  • 6
0

In addition to above format localization comment, in the template you can formatting numbers as follows.

<!DOCTYPE html>
{% load humanize %}

{% for post in posts %}
Views : {{ post.views|intcomma }}
{% endfor %}
Alex Jin Choi
  • 80
  • 3
  • 8