How to assign a unique ID to each date-picker field in a form in Django

1

I'm using django-bootstrap3 (pip install django-bootstrap3) to render forms and django-bootstrap-datepicker (pip install django-bootstrap-datepicker) to apply a calendar widget to DateField fields.

In my form, I have the fields check_in and check_out to which I want to apply together the effect of the date-picker, for which I do this:

from bootstrap_datepicker.widgets import DatePicker
class DateInput(DatePicker):
    def __init__(self):
        DatePicker.__init__(self,format="%Y-%m-%d")
    def build_attrs(self, attrs, extra_attrs=None, **kwargs):
        attrs = dict(self.attrs, **kwargs)
        if extra_attrs:
            attrs.update(extra_attrs)
        return attrs

class LodgingOfferForm(forms.ModelForm):

    class Meta:
        widgets = {
            'check_in': DateInput(),
            'check_out': DateInput(),
        }
        model = LodgingOffer
        fields = ('other fields', 'check_in', 'check_out', )

In my template I am rendering the fields in this way, including form.media to accept css and js effects in them. Broadly speaking I have it like this:

{% load bootstrap3 %}
{% block body_content %}

{% block extrahead %}   {# Extra Resources Start #}
   {{ form.media }}        {# Form required JS and CSS #}
{% endblock %}

{% bootstrap_field form.check_in %}
{% bootstrap_field form.check_out %}
{% endblock %}

When I render my form, the date-picker effect only applies to the first rendering field, that is, only for check_in and not for check_out

  • Check in

  • Check out

How can I apply the date-picker effect to both fields?

UPDATE

Performing an inspect in my browser, I have detailed that the execution time is generated by a JS code which assigns the id _pickers for each field check_in and check_out, only that the script calls the first one it finds and that's why that for the check_out field that is the second one does not work:

  • Check in

  • Check out

According to this, the same id _pickers is being used for the fields. How can I assign a unique id to each date-picker?

    
asked by bgarcial 15.02.2018 в 00:59
source

1 answer

0

I have changed the id pickers in this way: In the form definition I have overwritten the field by assigning an id selector with a different name.

class LodgingOfferForm(forms.ModelForm):
    class Meta:
        widgets = {
            'check_in': DateInput(),
            'check_out': forms.DateInput(attrs={'id': 'datepicker2'}),

            'country': CountrySelectWidget(),
        }
        model = LodgingOffer
        fields = ('check_in', 'check_out', )

And in my template I have overwritten or called the datepicker function in this way:

<script type="text/javascript">
    $(function () {
        $('#datepicker2').datepicker({
            inline: true,
            sideBySide: true,
           // format: 'DD.MM.YYYY' 
        });
    });
</script>

This way I already have different ids in each date field and the date-picker widget effect

    
answered by 17.02.2018 / 13:48
source