我有一个名为Member的模型,它包含一个日期字段。 在尝试设置页面样式时,我意识到我需要一些关于该领域的CSS课程。我遇到了这样的问题。
这是我的forms.py AddMember类:
class AddMember(ModelForm):
class Meta:
model = Member
exclude = ['id', 'member_id', 'CurrentRank', 'Lat', 'Lng']
widgets = {
'date_joined': forms.TextInput(attrs={'class': 'form-control form-control-inline input-medium date-picker'})
}
help_texts = {
'first_name': 'The First Name of the new recruit',
'middle_name': 'The Middle Name or Initial of the new recruit',
'last_name': 'The Last Name of the new recruit',
'home_phone': 'for US, us xxx-xxx-xxxx for international use country code + city code + number',
'cell_phone': 'Optional',
'fax_number': 'Optional',
'street1': 'House number and Street',
'street2': 'c/o or organization',
'City': 'City',
'State': 'State',
'Zip': 'Zip Code',
'Country': 'Select Country from drop down',
'Eye_Color': 'Eye Color',
'Hair_Color': 'Hair Color',
'Height': 'In Inches',
'date_joined': 'When the member joined first time'
}
它加入了模范成员:
class Member(models.Model):
id = models.AutoField(primary_key=True)
member_id = models.CharField(unique=True, max_length=30)
first_name = models.CharField(max_length=30)
middle_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.CharField(max_length=50)
Eye_Color = models.CharField(null=True, max_length=30)
Hair_Color = models.CharField(null=True, max_length=30)
Height = models.IntegerField(blank=True, null=True)
home_phone = PhoneNumberField(blank=True)
cell_number = PhoneNumberField(blank=True)
fax_number = PhoneNumberField(blank=True)
street1 = models.CharField(max_length=255, blank=True)
street2 = models.CharField(max_length=255, blank=True)
City = models.CharField(max_length=100, blank=True)
State = models.CharField(max_length=2, blank=True)
Zip = models.CharField(max_length=5, blank=True)
Country = CountryField()
Lat = models.FloatField(blank=True, null=True)
Lng = models.FloatField(blank=True, null=True)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
User = models.ForeignKey(User, blank=True, null=True)
从那里开始,我把它放在模板中:
<form role="form" action="/members/add/" method="post" class="form-horizontal">
{% csrf_token %}
<div class="form-body">
{% for field in form %}
<div class="form-group form-md-line-input">
<label class="col-md-2 control-label" for="{{ field.name }}">{{ field.label }}</label>
<div class="col-md-10">
{{field|addcss:"form-control"}}
<div class="form-control-focus">
</div>
{% if field.help_text %}
<span class="help-block">{{ field.help_text|safe }}</span>
{% endif %}
</div>
</div>
{% endfor %}
</div>
<div class="form-actions">
<div class="row">
<div class="col-md-offset-2 col-md-10">
<button type="button" class="btn default">Cancel</button>
<button type="Submit" class="btn blue">Submit</button>
</div>
</div>
</div>
</form>
我需要字段&#39; date_joined&#39;拥有添加的css类,以便前端可以启动日期的下拉选择。当我关闭页面时,它会像所有其他字段一样返回正常状态。
我错过了什么?
由于
更新1: 不久前,为了向对象添加默认的css标签,我创建了一个模板标签:
@register.filter(name='addcss')
def addcss(field, css):
return field.as_widget(attrs={"class": css})
我在模板中使用它。
显然,它超越了这个领域。如何在forms.py中设置此默认值并覆盖一个字段?感谢。