简体   繁体   English

将django表单(formset)中的十进制数加载到数字输入中

[英]Loading decimal numbers from django form (formset) into number input

I am creating a simple django app where you can create recipes with ingredients. 我正在创建一个简单的django应用程序,您可以使用成分创建食谱。 On my RecipeCreate view I have a simple formset with a select input (where you select a product) and a input type="number" with the product count. 在我的RecipeCreate视图中,我有一个带有select输入的简单formset(您选择产品的地方)和带有产品计数的input type="number" So my form and model are quite simple: 所以我的形式和模型很简单:

class Ingredient(models.Model):
    product = models.ForeignKey(Product)
    count = models.DecimalField(max_digits=4, decimal_places=2)

class IngredientForm(forms.ModelForm):
    class Meta:
        model = Ingredient
        fields = ('product', 'count') 

Now when i want to display my recipe on my RecipeEdit view everything loads fine except for the count input. 现在当我想在RecipeEdit视图上显示我的食谱时,除了count输入外,一切都正常。 I get the following jQuery validation error: 我得到以下jQuery验证错误:

The specified value "12,00" is not a valid number. The value 
must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?

Because I'm writing a Polish website I would like to have our national standard decimal separator, which is a , not a . 因为我正在写一个波兰网站,我想要我们的国家标准小数分隔符,这是一个,而不是一个. (but the dot should also be a separator). (但点也应该是分隔符)。

One of the solutions would be to use an input type="text" but it would mean I have to validate my number manually, which is troublesome. 其中一个解决方案是使用input type="text"但这意味着我必须手动验证我的号码,这很麻烦。 Is there any way to pass my number 12,00 so that it is accepted by the input field. 有没有办法传递我的号码12,00以便它被输入字段接受。 Probably changing/overwriting the standard jQuery validation? 可能更改/覆盖标准的jQuery验证? But I will accept any good suggested solution. 但我会接受任何好的建议解决方案。

Another curious fact is that when I write manually 12,00 or 12.00 and submit my form, my form is valid and my Ingredient count field has a value od 12 which is what I desire. 另一个奇怪的现象是,当我手动编写12,0012.00 ,并提交我的形式,我的方式是有效的,我的Ingredient计数字段的值OD 12这是我的愿望。 It's another pro for the input type="number" . 这是input type="number"的另一个专业人士。

You could set your locale decimal separator 您可以设置区域设置小数分隔符

UPDATE: 更新:

Didnt saw it is jquery related, 没看到它与jquery有关,

you could create new validator class for comma numbers for instance 例如,您可以为逗号编号创建新的验证程序类

jQuery.validator.addMethod("commanumber", function (value, element) {
    return this.optional(element) || /^(\d+|\d+,\d{1,2})$/.test(value);
}, "Please specify the correct number format");

or even extend regex to support . 甚至扩展正则表达式以支持。 also

Ok, I've done something like this. 好的,我做过这样的事情。 First of all I am using an input type="text". 首先,我使用输入类型=“文本”。 Then using jQuery.numeric() I convert my input so that it only accepts numbers, and a , as decimal separator. 然后使用jQuery.numeric()将我的输入,使其只接受数字,并且,作为小数点分隔符。

$("#field_id").numeric({ decimal : "," });

Now every time i submit my form I have some code that converts my , into . 现在,每当我提出我的表格时,我有一些代码,转换我的,. so that it fits the backend logic: 这样它适合后端逻辑:

$('#recipe-form').on('submit', function(){
    $('.convert-separator').each(function () {
        $(this).val($(this).val().replace(',','.'));
    });
    this.submit();
});

But I think it's not the best solution. 但我认为这不是最好的解决方案。 I think there must be a possibility to use the input type="number" field with a , separator. 我认为必须有使用的可能性input type="number"字段具有,分离器。

You can convert the count value in your jQuery code: 您可以在jQuery代码中转换计数值:

dotCount = count.toString().replace(',', '.');

This replaces the comma with a dot 这将用逗号替换逗号

I found nice article which one can describe your problem. 我发现很好的文章可以描述你的问题。 Author recommended to use lang attribute eg <html lang="en-150"> or <input type="number" lang="en-150"> . 作者建议使用lang属性,例如<html lang="en-150"><input type="number" lang="en-150">

There is also a note on www.w3.org www.w3.org上还有一个说明

4.10.5.2 Implemention notes regarding localization of form controls 4.10.5.2关于表单控件本地化的实现说明

This section is non-normative. 本节不具有规范性。

The formats shown to the user in date, time, and number controls is independent of the format used for form submission. 在日期,时间和数字控件中向用户显示的格式独立于用于表单提交的格式。

Browsers are encouraged to use user interfaces that present dates, times, and numbers according to the conventions of either the locale implied by the input element's language or the user's preferred locale. 鼓励浏览器根据输入元素语言或用户首选语言环境隐含的语言环境的约定,使用显示日期,时间和数字的用户界面。 Using the page's locale will ensure consistency with page-provided data. 使用页面的语言环境将确保与页面提供的数据保持一致。

For example, it would be confusing to users if an American English page claimed that a Cirque De Soleil show was going to be showing on 02/03, but their browser, configured to use the British English locale, only showed the date 03/02 in the ticket purchase date picker. 例如,如果美国英语页面声称将在02/03展示Cirque De Soleil节目,那么用户会感到困惑,但是他们的浏览器配置为使用英国英语语言环境,只显示日期03/02在购票日期选择器中。 Using the page's locale would at least ensure that the date was presented in the same format everywhere. 使用页面的语言环境至少可以确保日期以相同的格式呈现在任何地方。 (There's still a risk that the user would end up arriving a month late, of course, but there's only so much that can be done about such cultural differences...) (当然,用户最终可能会迟到一个月到达,但是对于这种文化差异,只能做很多事情......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM