Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show datepicker calendar on datefield

This is regarding how to use a jQuery date picker in a django powered site.

models.py is

from django.db import models class holidaytime(models.Model):     holiday_date = models.DateField() 

I am using jquery datepicker.

I am using a model form and created text field for displaying date sucessfully.

But failed to show a datepicker icon. I am using Django model forms.

like image 837
Monk L Avatar asked May 03 '13 10:05

Monk L


People also ask

How can I show Datepicker calendar on date field?

You can use widget to pass a class to the form when it will be rendered in the html. Then that class will be read by javascript to render the datepicker function. You probably should use a class, not an id. What if you have more than one copy of this form on the page, or two dates in the same form?

What is the function DateField () mean?

DateField is a field that stores date, represented in Python by a datetime. date instance. As the name suggests, this field is used to store an object of date created in python. The default form widget for this field is a TextInput. The admin can add a JavaScript calendar and a shortcut for “Today” explicitly.

What is widget in Django?

A widget is Django's representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. The HTML generated by the built-in widgets uses HTML5 syntax, targeting <! DOCTYPE html> .

How do you use a date picker?

Display the current date and time in a date pickerClick the Data tab. In the Data type box, click Date and Time (dateTime). Click Format. In the Date and Time Format dialog box, in the Display the time like this list, click the option that you want, and then click OK.


2 Answers

You can use widget to pass a class to the form when it will be rendered in the html. Then that class will be read by javascript to render the datepicker function.

Here is an example:

from django import forms class HolidayTimeForm(forms.Form):     holiday_date = forms.DateField(widget=forms.TextInput(attrs=                                 {                                     'class':'datepicker'                                 })) 

... or through the widgets attribute in the Meta class when using ModelForm:

class HolidayTimeForm(forms.ModelForm):      class Meta:         model = Holiday         widgets = {             'holiday_date': forms.DateInput(attrs={'class':'datepicker'}),         } 

Now in template:

 /* Include the jquery Ui here */  <script>   $(function() {     $( ".datepicker" ).datepicker({       changeMonth: true,       changeYear: true,       yearRange: "1900:2012",       // You can put more options here.      });   });   </script> 
like image 157
Amit Yadav Avatar answered Oct 09 '22 06:10

Amit Yadav


You can use Bootstrap-datetimepicker (not datepicker), here is an example:

  1. Add necessary script & file into your template:

<link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">  <link href="//cdn.bootcss.com/bootstrap-datetimepicker/4.17.44/css/bootstrap-datetimepicker.min.css" rel="stylesheet">  <script src="//cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>  <script src="//cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  <script src="//cdn.bootcss.com/moment.js/2.17.1/moment.min.js"></script>  <script src="//cdn.bootcss.com/bootstrap-datetimepicker/4.17.44/js/bootstrap-datetimepicker.min.js"></script>
  1. add class/id to your datetime fields by using widget:

    class MyTaskForm(forms.ModelForm): class Meta:    model = MyTask fields = ['completeDateTime'] widgets = {     'completeDateTime': forms.DateTimeInput(attrs={'class': 'datetime-input'}) } 
  2. Add the script to your template/html file: (NOTICE the format, it's import to use the same format of the Django)

<script>      $(function () {          $('.datetime-input').datetimepicker({              format:'YYYY-MM-DD HH:mm:ss'          });      });  </script>
like image 41
Lushang Avatar answered Oct 09 '22 06:10

Lushang