views:

45

answers:

2

Hi,
Using the django admin application for a date it displays Today | CalendarSymbol. I am using a datefield in my form and I thought it would do this for me how would i go about doing this? As i would normally use the jquery datepicker plugin is this the way to go?
Thanks in Advance,
Dean

+1  A: 

Usually I do something like this:

class CalendarWidget(forms.TextInput):
    class Media:
        css = {
            'all': ('datepicker.css',)
        }
        js = ('datepicker.js')

    def __init__(self, attrs={}):
        super(CalendarWidget, self).__init__(
            attrs={'class': 'datepicker', 
                   'size': '14', 
                   'readonly':'readonly'})

class ToDoForm(forms.ModelForm):
    class Meta:
        model = yourModel
    your_date_field = forms.DateTimeField(widget=CalendarWidget)

Finally in your template, something like this:

$(".datepicker").datepicker();

I hope it helps.

juanefren
And what about the js and CSS mentioned in the widget class do i need to include these or are they already in django?
Dean
You have to include your calendar media (for example the jquery calendar)... remember to add {{ form.media }} in your <head>
juanefren
A: 

I used the admin like widget in the end was easy to implement plus no external libraries.

Dean