优雅的方式在前端显示逗号分隔的整数,并在后端将其作为非逗号分隔(最好使用Python / Django)

时间:2022-09-02 00:21:38

In my line of work, I wrote custom accounting software in Web-Application form, which means that I deal a lot of numbers. Where I am located ( Indonesia ), our currency is big, 13000 Rupiah is equals to 1 USD. Needless to say, I want to know if there is an elegant solution to display a comma separated integer as the user type a value in the input field AND then parsing it out after submitting it to the backend.

在我的工作中,我用Web-Application形式编写了自定义会计软件,这意味着我处理了很多数字。我所在的位置(印度尼西亚),我们的货币很大,13000卢比等于1美元。毋庸置疑,我想知道是否有一个优雅的解决方案来显示逗号分隔的整数,因为用户在输入字段中键入一个值,然后在将其提交到后端后将其解析出来。

I have used the autonumeric plugin before ( http://www.decorplanit.com/plugin ), and parse it in the backend after submitting the form.

我之前使用过自动数字插件(http://www.decorplanit.com/plugin),并在提交表单后在后端解析它。

However, is there an easier way ( or more elegant ) way?

但是,有更简单的方式(或更优雅)吗?

Thank you in advance!

先感谢您!


Here's a bit more details on what I tried

这里有一些关于我尝试过的细节

Here's my template:

这是我的模板:

`{% load humanize %}` 
`{{ form.value|bootstrap|intcomma }}` 

And my model: value = models.BigIntegerField(null=True)

我的模型:value = models.BigIntegerField(null = True)

The generated Html: <input class=" form-control" id="id_value" max="9223372036854775807" min="-9223372036854775808" name="value" type="number">

生成的Html:

Since there's that type="number", I am unable to see the commas in the frontend.

由于那个type =“number”,我无法在前端看到逗号。

1 个解决方案

#1


0  

You can store values in DB as normal numbers and use filters to represent them in templates however you want.

您可以将数据库中的值存储为普通数字,并使用过滤器在模板中表示它们。

Filter for you can be found in humanize and it's called intcomma

您可以在humanize中找到过滤器,它称为intcomma

Here is how it works:

下面是它的工作原理:

{% load humanize %}

{{ value|intcomma }}

If the value is 42000, in the front-end you will see 42,000

如果值为42000,则在前端您将看到42,000

#1


0  

You can store values in DB as normal numbers and use filters to represent them in templates however you want.

您可以将数据库中的值存储为普通数字,并使用过滤器在模板中表示它们。

Filter for you can be found in humanize and it's called intcomma

您可以在humanize中找到过滤器,它称为intcomma

Here is how it works:

下面是它的工作原理:

{% load humanize %}

{{ value|intcomma }}

If the value is 42000, in the front-end you will see 42,000

如果值为42000,则在前端您将看到42,000