S
S
Stanislav2017-05-05 18:41:58
Django
Stanislav, 2017-05-05 18:41:58

How to override template_names in form widget in Django 1.11?

You need to change the display of the SelectDateWidget date fields - wrap the year, month, day fields in div tags with different classes. In django 1.10 this was done by overriding the render method with a few lines added

output = []
    for field in self._parse_date_fmt():
        if field == 'year':
            output.append('<div class="input-field col s4">' + html['year'] + '</div>')
        elif field == 'month':
            output.append('<div class="input-field col s5">' + html['month'] + '</div>')
        elif field == 'day':
            output.append('<div class="input-field col s3">' + html['day'] + '</div>')
    return mark_safe('\n'.join(output))

In Django 1.11 this method does not work. Tried to override template_name but nothing changes.
widgets.py
class CustomDateWidget(SelectDateWidget):
    def get_template_names(self):
        return ['accounts/custom_select_date.html']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav, 2017-05-07
@Chebaa

The solution was suggested by Dmitry Burnaev

SelectDateWidget.template_name = 'accounts/custom_select_date.html'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question