在SelectDateWidget中为每个选择添加div包装器

时间:2014-08-08 11:10:57

标签: django django-forms

是否可以覆盖SelectDateWidget以围绕每个选择包装div?

像这样:

<div class="wrapper">
<select class="selectdatewidget form-control" id="id_birthdate_day" name="birthdate_day">
<option value="0">---</option>....
</select>
</div>

<div class="wrapper">
<select class="selectdatewidget form-control" id="id_birthdate_month" name="birthdate_month">
<option value="0">---</option>....
</select>
</div>

<div class="wrapper">
<select class="selectdatewidget form-control" id="id_birthdate_year" name="birthdate_year">
<option value="0">---</option>....
</select>
</div>

2 个答案:

答案 0 :(得分:0)

您可以继承SelectDateWidget并覆盖其中的render方法。之后,您可以在表单中使用CustomSelectDateWidget。

例如:

class CustomSelectDateWidget (SelectDateWidget):
"""Override of the standard renderer used for SelectDateWidget"""    

    def render(self, name, value, attrs=None):
        try:
            year_val, month_val, day_val = value.year, value.month, value.day
        except AttributeError:
            year_val = month_val = day_val = None
            if isinstance(value, six.string_types):
                if settings.USE_L10N:
                    try:
                        input_format = get_format('DATE_INPUT_FORMATS')[0]
                        v = datetime.datetime.strptime(value, input_format)
                        year_val, month_val, day_val = v.year, v.month, v.day
                    except ValueError:
                        pass
                else:
                    match = RE_DATE.match(value)
                    if match:
                        year_val, month_val, day_val = [int(v) for v in match.groups()]
        choices = [(i, i) for i in self.years]
        year_html = self.create_select(name, self.year_field, value, year_val, choices)
        choices = list(six.iteritems(MONTHS))
        month_html = self.create_select(name, self.month_field, value, month_val, choices)
        choices = [(i, i) for i in range(1, 32)]
        day_html = self.create_select(name, self.day_field, value, day_val,  choices)

        output = []
        for field in _parse_date_fmt():
            if field == 'year':
                output.append('<div class="wrapper">' + year_html + '</div>')
            elif field == 'month':
                output.append('<div class="wrapper">' + month_html + '</div>')
            elif field == 'day':
                output.append('<div class="wrapper">' + day_html + '</div>')
        return mark_safe('\n'.join(output))`

重要的一点是代码的结尾。这应该工作,但不测试。我建议从Django中自己的SelectDateWidge版本中复制render方法(我正在运行Django 1.5)并覆盖附加了div的部分。

答案 1 :(得分:0)

为了扩展@BogdiG所说的内容,将包装器作为字符串传递给窗口小部件会更有用,这样它就更通用了。可重复使用的。

class MySelectDateWidget(SelectDateWidget):

    def __init__(self, attrs=None, years=None, required=True, wrapper='%s'):
        self.wrapper = wrapper


    def render(self, name, value, attrs=None):

        # don't forget to put the rest of the render()
        # in here if you want to copy/paste this

        output = []
        for field in _parse_date_fmt():
            if field == 'year':
                output.append(self.wrapper % year_html)
            elif field == 'month':
                output.append(self.wrapper % month_html)
            elif field == 'day':
                output.append(self.wrapper % day_html)
        return mark_safe('\n'.join(output))`


class MyForm(forms.ModelForm):
    dob = forms.DateField(
        label=_('Date of birth'),
        widget=SelectDateWidget(wrapper='<div class="wrapper">%s</div>')
    )