0
我有一個名爲Member的模型,其中包含一個日期字段。 雖然試圖風格的頁面,我意識到我需要在該領域的幾個CSS類。我遇到問題了。如何將css添加到Django ModelForm中的特定字段中
這裏是我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>
我需要字段'date_joined'來添加CSS類,以便前端可以激活日期的下拉選擇。當我關閉頁面時,它會像所有其他字段一樣恢復正常。
我錯過了什麼?
感謝
更新1: 較早前,在默認的CSS標籤添加到對象,我創建一個模板標籤:
@register.filter(name='addcss')
def addcss(field, css):
return field.as_widget(attrs={"class": css})
我使用它在模板中。
顯然它是壓倒了領域。如何在forms.py中設置這個默認值並覆蓋這個字段?
謝謝。