-1

How get Time picker django form. Here is my code. Thank you

My model:

    class activite(models.Model):
        jour = models.ForeignKey('jour',on_delete=models.CASCADE, related_name='jour' )
        debut = models.TimeField(blank=True, null=True)
        fin = models.TimeField(blank=True, null=True)

My form:

class activiteForm(forms.ModelForm):  
    class Meta:
        model = fdt_activite
        fields = ('jour','debut','fin')

My template:

    <form method="POST" class="post-form">
    {% csrf_token %}
    {{form.as_p}}
    <button type="submit" class="save btn btn-default">Création activité</button>
    </form>

    <script>
    $('#debut').datetimepicker({
       showSecond: true,
       showMillisec: true,
       timeFormat: 'hh:mm:ss'
    });
    </script>
Bak
  • 411
  • 1
  • 5
  • 19

1 Answers1

1
$('#debut') <- This id is incorrect

You've to pass the actual id of the debut field here.

Django automatically creates id for form fields like this: id_<field-name>. So, the id for debut field will be id_debut.

To fix it, use that in your JS:

$('#id_debut')
xyres
  • 20,487
  • 3
  • 56
  • 85
  • I added the JS: $ ('# id_debut') but no change. Do I have to referecence like this example? – Bak Mar 14 '18 at 13:26
  • @Bak Well, you'll also have to include the required JS files. Read the docs of the plugin that you're using. I'm sure it must have a nice explanation about setting things up. – xyres Mar 14 '18 at 13:50