python django:使用管理员日期选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/660898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:34:13  来源:igfitidea点击:

django : using admin datepicker

pythondjangoformsdjango-formsdate

提问by interstar

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

我正在尝试在我自己的 Django 表单中使用管理员日期选择器。

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

大致遵循此处的讨论:http: //www.mail-archive.com/[email protected]/msg72138.html

I've

我有

a) In my forms.py included the line

a) 在我的 forms.py 中包含了这一行

from django.contrib.admin import widgets

b) and used the widget like this :

b)并使用这样的小部件:

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

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

c) 在我的实际模板中,我添加了:

{{form.media}}

To include the js / styles etc.

包括 js / 样式等。

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 :

然而,当我尝试查看我的表单时,我没有得到很好的小部件;只是一个普通的文本框。Firefox javascript 错误控制台显示给我:

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

在 calendar.js 中没有定义 gettext(第 26 行)

and

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

在 DateTimeShortcuts.js(第 254 行)中未定义 addEvent

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

有什么建议?这是 Django 自己的 javascript 库中的错误吗?

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

更新:基本上,需要包括核心和(或假的)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)

更新 2:Carl 指出这几乎是Using Django time/date widgets in custom form(虽然从不同的位置开始)的重复

回答by Vasil

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.

它试图在 js 中调用 gettext() 国际化函数。您可以像在 python 代码或模板中那样进行 js 国际化,这只是一个鲜为人知的功能。

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

如果你不在你的项目中使用 js 国际化,你可以放。

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

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

在您的顶级模板中,这样 js 解释器就不会窒息。

This is a hacky way to solve it I know.

我知道这是一种解决它的hacky方法。

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.

或者,您可以包含确切的 jsi18n js django 管理引用,以使其即使使用其他语言也能正常工作。我不知道是哪一种。

This was posted on django-users today:

这是今天发布在 django-users 上的:

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

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

Maybe it was you, anyway, just in case.

也许是你,无论如何,以防万一。

回答by interstar

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

但它仍然报告不知道gettext

回答by Aidan Fitzpatrick

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 }}