简体   繁体   English

如何在 Django 表单中为 IntegerField 设置 NumberInput 小部件的宽度和输入限制

[英]How to set width and input limit of NumberInput widget for IntegerField in a Django form

I'm looking for a way to change the width of the input box in a Django form.我正在寻找一种方法来更改 Django 表单中输入框的宽度。

The model code for a parameter looks like this:参数的模型代码如下所示:

my_integer = models.IntegerField('Window', default=50)

And, putting it into a form, the HTML code that is generated is this:并且,将其放入表单中,生成的 HTML 代码是这样的:

<input id="id_my_integer" name="my_integer" value="50" type="number">

And, the box is quite wide - maybe 20 characters.而且,盒子很宽——可能有 20 个字符。

If I add the code in the definition of the form class Meta:如果我在表单类 Meta 的定义中添加代码:

widgets = {
  'my_integer': forms.TextInput(attrs={'size':'3', 'maxlength':'3'}),
}

I get a box that is 5-6 characters wide that lets me insert 3 characters and the HTML code changes the type to type='text' and the increment/decrement arrows disappear.我得到一个 5-6 个字符宽的框,可以让我插入 3 个字符,HTML 代码将类型更改为 type='text' 并且递增/递减箭头消失。

So, what I really want is a 3 character wide box, with a 3 character input limit, having the NumberInput widget's increment/decrement arrows.所以,我真正想要的是一个 3 个字符宽的框,输入限制为 3 个字符,具有NumberInput小部件的递增/递减箭头。 How do I do this?我该怎么做呢?

The arrow controls are a part of the NumberInput widget, so you'll have to use that. 箭头控件是NumberInput小部件的一部分,因此您必须使用它。 Unfortunately, in current HTML, there is no support for the size and maxlength attributes for the number input field <input type="number"> . 不幸的是,在当前的HTML中,不支持数字输入字段<input type="number">sizemaxlength属性。

The width of the field is easier to hack, you can simply override the CSS. 字段的宽度更容易被破解,您可以简单地覆盖CSS。 You can use either px or ch as units, depending on what your requirement is. 您可以使用pxch作为单位,具体取决于您的要求。 To have a 3 character wide input box, add 'style': 'width:3ch' to the attrs dictionary. 要有一个3个字符宽的输入框,请在attrs字典中添加'style': 'width:3ch' I'm using 6ch in the example below because using exactly 3ch leaves no space for the increment-decrement buttons. 我在下面的示例中使用了6ch ,因为正好使用3ch不会为增量递减按钮留下空间。

To limit the number of characters entered, a simple js script will work. 要限制输入的字符数,可以使用简单的js脚本。 Here's the entire solution. 这是整个解决方案。

The widget: 小部件:

widgets = {
    'my_integer': forms.NumberInput(attrs={
        'id': 'number_field',    
        'style': 'width:6ch',
        'oninput': 'limit_input()'
    }),
}

Inside your template file displaying the form, add this small script: 在显示表单的template文件中,添加以下小脚本:

<script type="text/javascript">
    function limit_input() {
        var field = document.getElementById("number_field");
        var max_length = 3;
        if (field.value.length > max_length) {
            field.value = field.value.slice(0, max_length); 
        }
    }
</script>

Now in Django 2.1.7 you can use size in NumberInput attrs 现在在Django 2.1.7中,您可以在NumberInput attrs中使用size

widgets = {
    'bank_account_number': form.NumberInput(attrs={'size': '20'}),
}

and just simply replace the attrs with attrs={'style': 'width:6ch'} works very well too. 只需用attrs={'style': 'width:6ch'}替换attrs也很有效。 thanks Yash Tewari! 谢谢Yash Tewari!

When I need to control the size of an input box and the number of characters, this is how I do it (I am citing a specific case involving an integer field here): 当我需要控制输入框的大小和字符数时,我就是这样做的(我引用了一个涉及整数字段的特定情况):

In models.py: 在models.py中:

from django.core.validators import MinValueValidator, MaxValueValidator

class SomeTable(models.Model):
    field1 = models.CharField(max_length=10)
    field2 = models.IntegerField(default=1, validators=[MinValueValidator(1), MaxValueValidator(9999),], )

In forms.py: 在forms.py中:

from .models import SomeTable

class SomeForm(forms.ModelForm):
    class Meta:
        model = SomeTable
        fields = ('field1', 'field2')
        widgets = {
             'field2': forms.NumberInput(attrs={'style': 'width: 100px'}),
            }

As with a maximum value, (I have taken liberty to include it here) we may sometimes need to specify the minimum value as well, which has been shown in the sample codes above. 与最大值一样,(我已经自由地将其包含在此处)我们有时也需要指定最小值 ,这已在上面的示例代码中显示。 And the width of the integer field can be controlled at the forms level using the widget " NumberInput ". 并且可以使用窗口小部件“ NumberInput ”在窗体级别控制整数字段的宽度。 A reference here . 参考这里

As shown below, by setting "style"(for field width) , "max"(for maximum number) , "min"(for minimum number) to "forms.NumberInput()" , you can do what you want to do:如下图,通过设置"style"(for field width) , "max"(for maximum number) , "min"(for minimum number)"forms.NumberInput()" ,你可以做你想做的事:

widgets = {
    'my_integer': forms.NumberInput(attrs={
        'style': 'width:6ch', # For field width
        'max': '999',         # For maximum number
        'min': '0',           # For minimum number
    }),
}

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

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