以我的脆皮形式,我想插入一个尺寸=" 3"只在一个输入字段中,因此它将匹配其maxlength。在将bpm定义为CharField时,我尝试以与插入maxlength相同的方式插入大小但是它没有工作。
目前,在渲染时,会显示
<input class="textinput textInput form-control" id="id_bpm" maxlength="3" name="bpm" type="text" />
但我想将其更改为显示
<input class="textinput textInput form-control" id="id_bpm" maxlength="3" size="3" name="bpm" type="text" />
在我的表单模板中,我调用{%crispy form%}
在我的forms.py中:
from django import forms
from .models import Profile, Artist
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Div
from crispy_forms.bootstrap import StrictButton, FormActions
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = [
"artist",
"title",
"mix",
"bpm",
"genre",
]
artist = forms.CharField(widget=forms.TextInput)
bpm = forms.CharField(widget=forms.TextInput, max_length=3)
def __init__(self, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-lg-2'
self.helper.field_class = 'col-lg-10'
self.helper.layout = Layout(
'artist',
'title',
'mix',
'bpm',
'genre',
FormActions(Div(Submit('submit','Submit', css_class='btn-primary'), style="float: right")),
)
def clean_artist(self):
artist = self.cleaned_data.get("artist")
if not artist:
raise forms.ValidationError("Artist is a required field.")
else:
artist, created = Artist.objects.get_or_create(name=artist)
return artist
答案 0 :(得分:0)
使用Field
布局对象为布局对象指定任何attributes:
from crispy_forms.layout import Field
self.helper.layout = Layout(
'artist',
'title',
'mix',
Field('bpm', size=3),
'genre',
# ...
)