Skip to content
Advertisement

How to have Calendar thing in Django date field

I have this Datetime field in Django

update_date     = models.DateField()

But I want the Calendar to pop up. In docs, they say to write this

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

Now I have a few problems:

  1. Where to declare this class and how to use it
  2. I don’t have pretty.css , animation.css , how can get it
  3. Do I need to do programming for it??

Advertisement

Answer

The files ‘pretty.css’, ‘animations.js’ and ‘actions.js’ are not included with Django, this code in the docs is an example only. It is your duty to write the code for fancier widgets – personally I like jQuery widgets (see the reference links bellow). From the docs:

Which JavaScript toolkit? Many JavaScript toolkits exist, and many of them include widgets (such as calendar widgets) that can be used to enhance your application. Django has deliberately avoided blessing any one JavaScript toolkit. Each toolkit has its own relative strengths and weaknesses – use whichever toolkit suits your requirements. Django is able to integrate with any JavaScript toolkit.

Supposing you got all the css and js defined for your fancy widget, in the ModelForm you have to override the field widget like this:

class SomeForm(forms.ModelForm):
    ...
    update_date = forms.CharField(widget=CalendarWidget)
    ...

Don’t forget to display the form media at the template header.

Look these answers:

  1. What’s the cleanest, simplest-to-get running datepicker in Django?
  2. https://groups.google.com/group/django-users/browse_thread/thread/8bd4675ba642ebcb?pli=1
Advertisement