django : using admin datepicker


I'm trying to use the admin datepicker in my own django forms.

Roughly following the discussion here :


a) In my included the line

from django.contrib.admin import widgets

b) and used the widget like this :

date = forms.DateTimeField(widget=widgets.AdminDateWidget())

c) And in my actual template I've added :


To include the js / styles etc.

However, when I try to view my form I get no nice widget; just an ordinary text box. And the Firefox javascript error console shows me :

gettext is not defined in calendar.js (line 26)


addEvent is not defined in DateTimeShortcuts.js (line 254)

Any suggestions? Is this a bug in Django's own javascript library?

Update : Basically, need to include the core and (or fake) the i18lization

Update 2 : Carl points out this is pretty much a duplicate of Using Django time/date widgets in custom form (although starting from a different position)

5/23/2017 12:25:02 PM

No, it's not a bug.

It's trying to call the gettext() internationalization function in js. You can do js internationalization much like you do it in python code or templates, it's only a less known feature.

If you don't use js internationalization in your project you can just put.

<script>function gettext(txt){ return txt }</script>

in your top template so the js interpreter doesn't choke.

This is a hacky way to solve it I know.


Or you can include the exact jsi18n js django admin references to get it working even with other languages. I don't know which one it is.

This was posted on django-users today:

Maybe it was you, anyway, just in case.

8/1/2013 3:21:12 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow