django custom date_field widget by datepicker

366 views Asked by At

So, I want to use a JS library called datepicker as a template for my date fields in forms.

My custom widget`s code just renders a template:

class MyDateWidget(SelectDateWidget):

    def render(self, name, value, attrs=None):
        template = loader.get_template('date_widget.html')
        return template.render(Context({}))

and date_widget.html contains following

<div class="input-append date" id="dp-input1" data-date-format="dd-mm-yyyy">
  <input class="span12" size="16" type="text" value="12-02-2012" readonly="">
  <span class="add-on"><i class="icon-calendar" onclick="$('#dp-input1').datepicker();"></i></span>
</div>

this html is kind of ok, I can see the nice popup datepicker menu in my page, but for some reason request.POST (this form posts, yes) doesn`t contain the picked date. Other fields are presented into the POST data.

Apparently my widget misses some important "backend" logic.

1

There are 1 answers

0
seorc On

Though this question is pretty old, I think you must add the name attribute to the input tag:

class MyDateWidget(SelectDateWidget):

def render(self, name, value, attrs=None):
    template = loader.get_template('date_widget.html')
    return template.render(Context({'name': name}))

And:

<div class="input-append date" id="dp-input1" data-date-format="dd-mm-yyyy">
    <input class="span12" size="16" type="text" name="{{ name }}" value="12-02-2012" readonly="">
    <span class="add-on"><i class="icon-calendar" onclick="$('#dp-input1').datepicker();"></i></span>
</div>