如何在不保留唯一数字输入功能的情况下从 Django integerField 中删除箭头?
Posted
技术标签:
【中文标题】如何在不保留唯一数字输入功能的情况下从 Django integerField 中删除箭头?【英文标题】:How to remove arrows from Django integerField without maintaining the only number input feature? 【发布时间】:2022-01-14 13:08:10 【问题描述】:forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
registration = forms.IntegerField(label='Registration Number')
class Meta:
model = User
fields = ['username', 'email','registration','password1', 'password2']
如何从注册字段中删除向上和向下箭头? 附言我只想让用户在这个字段中输入数字。
【问题讨论】:
【参考方案1】:这可以在 CSS 中完成:
像这样:
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button
-webkit-appearance: none;
margin: 0;
您应该能够通过以下方式使其特定于该字段:
#id_registration::-webkit-outer-spin-button,
#id_registration::-webkit-inner-spin-button
-webkit-appearance: none;
margin: 0;
【讨论】:
【参考方案2】:您可以通过覆盖Meta
类中的小部件来做到这一点。
像这样:
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
# Import widget that you want
from django.forms import TextInput
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
registration = forms.IntegerField(label='Registration Number')
class Meta:
model = User
fields = ['username', 'email','registration','password1', 'password2']
widgets =
'registration': TextInput()
我也不确定,但你可以试试这个:
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
# Add widget to your field
registration = forms.IntegerField(label='Registration Number', widget=forms.TextInput())
class Meta:
model = User
fields = ['username', 'email','registration','password1', 'password2']
更多信息:
Overriding the default fields
【讨论】:
以上是关于如何在不保留唯一数字输入功能的情况下从 Django integerField 中删除箭头?的主要内容,如果未能解决你的问题,请参考以下文章