在django(1.5.4)模型表单中我如何获得管理类型的日期和时间选择器?

时间:2014-10-30 09:39:25

标签: django django-models django-forms

from django import form
from awesomite.models import tasks

    class todoform(forms.ModelForm):

        title = forms.CharField(max_length=250, help_text="Please enter a title")
        description = forms.CharField(max_length=400, help_text="Please enter title description")
        time = forms.DateTimeField()

        class Meta:
            model = tasks
            fields = ('title','description','time')

在上面的代码中,我希望在显示表单时有一个日期选择器和时间选择器(如管理页面中所示)。我在其他参考文献中搜索了这个,发现很难实现或遵循。我是django的新手。

1 个答案:

答案 0 :(得分:1)

尝试:

from django import form
from awesomite.models import tasks
from django.contrib.admin import widgets

class todoform(forms.ModelForm):

    title = forms.CharField(max_length=250, help_text="Please enter a title")
    description = forms.CharField(max_length=400, help_text="Please enter title description")
    time = forms.DateTimeField(widget=widgets.AdminSplitDateTime())

    class Meta:
        model = tasks
        fields = ('title','description','time')

您应该在表单>媒体类中或直接在模板

中包含其他媒体文件

(参见 - > Using Django time/date widgets in custom form