DatepickerWidget in CreateView

放肆的年华 提交于 2019-12-10 20:12:44

问题


In my model I have a Datefield. So I want to use a Datepicker. How to use the Django-Admin Datepicker?

I have found examples to do this in a Form, but I have only desined a model. Is it possible to define this widget in my Model?


回答1:


You can use get_form method to override widget attribute:

class MyCreateView(CreateView):
    def get_form(self, form_class):
        form = super(MyCreateView, self).get_form(form_class)
        form.fields['date_field'].widget.attrs.update({'class': 'datepicker'})
        return form



回答2:


I'll suggest you to create a modelform, in that form file import the admin datepicker widget

from django.contrib.admin.widgets import AdminDateWidget

and define the widgets for that field using attrs=AdminDateWidget

and in template put {{ form.media }} to get include that widget javascript and css files in html



来源:https://stackoverflow.com/questions/21405895/datepickerwidget-in-createview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!