django中的自定义日期小部件?

时间:2014-06-19 05:18:48

标签: python django

在我目前的基于python和django的应用程序中,我为日期创建了一个自定义小部件。

from datetime import date
from django.forms import widgets

class DateSelectorWidget(widgets.MultiWidget):
    def __init__(self, attrs=None):
        # create choices for months, years
        # example below, the rest snipped for brevity.
        years = [(year, year) for year in range(1945, 2016)]        
        months = [(1,'Jan'),(2,'Feb')]
        _widgets = (        
            widgets.Select(attrs=attrs, choices=months),
            widgets.Select(attrs=attrs, choices=years),
        )
        super(DateSelectorWidget, self).__init__(_widgets, attrs)

    def decompress(self, value):
        if value:
            return [value.month, value.year]
        return [None, None]

    def format_output(self, rendered_widgets):
        return u''.join(rendered_widgets)

    def value_from_datadict(self, data, files, name):
        datelist = [
            widget.value_from_datadict(data, files, name + '_%s' % i)
            for i, widget in enumerate(self.widgets)]
        try:
            D = date(day=1, month=int(datelist[0]),
                    year=int(datelist[1]))
        except ValueError:
            return ''
        else:
            return str(D) 

在加载表单时返回工作正常(返回日期对象),但是当我提交表单并在表单中将某些字段留空时,我收到以下错误。

Caught AttributeError while rendering: 'str' object has no attribute 'month'

Request Method:     POST
Request URL:    
Django Version:     1.3.1
Exception Type:     TemplateSyntaxError
Exception Value:    

Caught AttributeError while rendering: 'str' object has no attribute 'month'

Exception Location:     /var/www/stacks/django-apps/kkk/apps/oooomonth_year.py in decompress, line 21

2 个答案:

答案 0 :(得分:0)

你在这里收到错误,

def decompress(self, value):
        if value:
            return [value.month, value.year]
        return [None, None]

value对象没有属性月份。如果您想知道值属性print dir(value)。在这个值中是一个字符串。

<强>更新

检查属性hasattr(value, 'month')。我希望这会对你有所帮助。

答案 1 :(得分:0)

当数据绑定到表单时它就出现了。 (未存储到DB中,因为验证消息或任何其他问题)。从那以后

def decompress(self, value):
        if value:
            return [value.month, value.year]
        return [None, None]

上述方法仅在日期存储到DB时处理日期。但是当表单未成功提交时,其行为类似于&#34; Str object&#34;。所以你需要通过以下定义的方法更新上面的方法:

def decompress(self, value):
        if value:
            import types
            try:
                if type(value) == types.StringType:
                    from datetime import datetime as dt
                    tmp_date = dt.strptime(value, '%Y-%m-%d')
                    return [tmp_date.month, tmp_date.year]
                else:
                    return [value.month, value.year]
            except:
                raise
        else:
            return [None, None]

现在,此方法将处理两种情况,无论是否提交。