13

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

Roughly following the discussion here : http://www.mail-archive.com/[email protected]/msg72138.html

I've

a) In my forms.py 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 :

{{form.media}}

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)

and

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)

4
  • This question is a duplicate. See the answer here: stackoverflow.com/questions/38601/…
    – Carl Meyer
    Commented Mar 19, 2009 at 9:22
  • thanks ... funny, that didn't pop up in any of "Related Questions" Either in the side-bar here or when I was writing the question
    – interstar
    Commented Mar 19, 2009 at 14:43
  • The answer to the question Carl Meyer references, makes clear that this is an answer which requires a lot of work, more than just implementing your own datepicker using jQuery or some other well-known library.
    – rossdavidh
    Commented Jan 18, 2012 at 15:35
  • A few years later, there are some alternatives now, like using the browser's built-in date picker. No javascript or css required.
    – djvg
    Commented Sep 16, 2021 at 7:00

3 Answers 3

5

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.

Edit:

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:

http://groups.google.com/group/django-users/browse_thread/thread/2f529966472c479d#

Maybe it was you, anyway, just in case.

2
  • thanks ... yes, it's hacky, but it's got me past that problem as a temporary fix
    – interstar
    Commented Mar 19, 2009 at 3:48
  • Thanks for the link, it was well written and made the process clear, and feel a little less hacky.
    – monkut
    Commented Aug 12, 2009 at 15:16
2

I think I solved the first half by explicitly adding these lines to my template :

<script type="text/javascript" src="../../../jsi18n/"></script> 
<script type="text/javascript" src="/admin_media/js/core.js"></script>
<script type="text/javascript" src="/admin_media/js/admin/RelatedObjectLookups.js"></script>

But it still reports not knowing gettext

3
  • 1
    there's no point in adding jsi18n if you don't have it in your url mappings. that's where the gettext function will be if you do add the right thing in urls.py: urlpatterns = patterns('', (r'^jsi18n/(?P<packages>\S+?)/$', 'django.views.i18n.javascript_catalog'), )
    – Vasil
    Commented Mar 19, 2009 at 3:49
  • good point .. though I found that line in the admin form too. So I presume it works on this server. In which case ... why for them if not me. Maybe because it's relative in the admin tree there.
    – interstar
    Commented Mar 19, 2009 at 4:05
  • :) yes, but it's a relative uri, so if you can figure out what it resolves to in the admin templates, you can put it as an absolute url and have it work. I think this way event proper internationalization of the datepicker will work.
    – Vasil
    Commented Mar 19, 2009 at 4:32
1

You may find the following works for you:

<link href="/media/css/base.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/media/js/core.js"></script>
{{ form.media }} 

Not the answer you're looking for? Browse other questions tagged or ask your own question.