简体   繁体   English

django管理表单中的DateInput

[英]DateInput in django admin form

I'm editing the form of my app admin section. 我正在编辑我的应用程序管理部分的表单。

model.py 模型

class Friend(models.Model):
id         = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
genre      = models.CharField(max_length=1, choices=(("M", "Male"), ("F", "Famale")))
birth_date = models.DateField()

admin.py 管理员

class FriendAdmin(admin.ModelAdmin):
  change_form_template = 'admin/change_form.html'
  change_list_template = 'admin/change_list.html'

admin.site.register(Friend, FriendAdmin)

form.py 表格

class FriendForm(admin.ModelAdmin):
  form    = Friend
  fields  = ('birth_date',)
  widgets = {'birth_date': forms.DateInput(attrs={'class': 'form-control'})}

I expect that my form contains only the "birth_date" field and that it has the "form-control" class and the "DateInput" type. 我希望我的表单仅包含“ birth_date”字段,并且具有“ form-control”类和“ DateInput”类型。 Nevertheless, my form contains all the fields and the "birth_date" is a simple text field. 但是,我的表单包含所有字段,而“ birth_date”是一个简单的文本字段。 What am I doing wrong? 我究竟做错了什么?

PS I've imported bootstrap, bootstrap-daterangepicker, jquery and moment library and I've added this code to my change_form.html page: PS我已经导入了bootstrap,bootstrap-daterangepicker,jquery和moment库,并将此代码添加到了change_form.html页面中:

<script type="text/javascript">
  $('.datepicker').datepicker();
</script>

Add class datepicker to your attributes 将类datepicker添加到属性

class FriendForm(admin.ModelAdmin):
  form    = Friend
  fields  = ('birth_date',)
  widgets = {'birth_date': forms.DateInput(attrs={'class': 'form-control datepicker'})}

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

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