未捕获的 ReferenceError:django 未定义

2022-01-18 00:00:00 python django widget forms javascript

我正在尝试在我的公式中使用带有几个 DateField 的管理小部件,但只有第一个可以正确显示小部件,而另一个我收到错误:

<块引用>

DateTimeShortcuts.js:205 Uncaught ReferenceError: django is not defined

(指示的行是:

django.jQuery(document).bind('keyup', function(event) {

)

这是我的模板头:

{% 加载管理媒体 %}<link rel="stylesheet" href="/static/css/main.css"/><script type="text/javascript" src="/admin/jsi18n/"></script><script type="text/javascript" src="/static/admin/js/core.js"></script><脚本类型="文本/javascript">window.__admin_media_prefix__ = "{% filter escapejs %}{% admin_media_prefix %}{% endfilter %}";</脚本><link rel="stylesheet" type="text/css" href="/static/admin/css/forms.css"/><link rel="stylesheet" type="text/css" href="/static/admin/css/base.css"/><!-- <link rel="stylesheet" type="text/css" href="/static/admin/css/global.css"/>--><link rel="stylesheet" type="text/css" href="/static/admin/css/widgets.css"/>{{ form.media }}...

我的模型字段:

start_date = models.DateField()end_date = models.DateField()

还有我的表单类:

类 UserForm(ModelForm):start_date = forms.DateField(小部件=小部件.AdminDateWidget)end_date = forms.DateField(小部件=小部件.AdminDateWidget)元类:模型 = 用户

我猜这是某种渲染规则,但我完全糊涂了.欢迎任何帮助!

解决方案

django.jQuery定义在django/contrib/admin/static/admin/js/jquery.init.js 并依赖于 django/contrib/admin/static/admin/js/jquery.js.你需要直接在你的模板中加载那些js文件

<script type="text/javascript" src="/static/admin/js/jquery.min.js"></script><script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>

或将这些文件包含在您的表单媒体中p>

同时检查 ModelAdminmedia 属性.

I'm trying to use the Admin Widgets with a couple of DateField on my formulary but only the first of them gets the widget displayed correctly, for the other one I get an error:

DateTimeShortcuts.js:205 Uncaught ReferenceError: django is not defined

(The line indicated is:

django.jQuery(document).bind('keyup', function(event) { 

)

Here's my template head:

<head>
{% load adminmedia  %}
<link rel="stylesheet" href="/static/css/main.css" />
<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript">
    window.__admin_media_prefix__ = "{% filter escapejs %}{% admin_media_prefix %}{% endfilter %}";
</script>
<link rel="stylesheet" type="text/css" href="/static/admin/css/forms.css"/>
<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css"/>
<!-- <link rel="stylesheet" type="text/css" href="/static/admin/css/global.css"/> -->
<link rel="stylesheet" type="text/css" href="/static/admin/css/widgets.css"/>
{{ form.media }}...

My model fields:

start_date = models.DateField()
end_date = models.DateField()

And my form class:

class UserForm(ModelForm):
    start_date = forms.DateField(
        widget=widgets.AdminDateWidget
        )
    end_date = forms.DateField(
        widget=widgets.AdminDateWidget
        )
    class Meta:
        model = User

I guess it some kind of rendering rule but I'm totally confused. Any help is welcome!

解决方案

django.jQuery is defined in django/contrib/admin/static/admin/js/jquery.init.js and relies on django/contrib/admin/static/admin/js/jquery.js. You need to load those js files in your template directly

<script type="text/javascript" src="/static/admin/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>

Or include those files in your form media

Also check the media property of ModelAdmin.

相关文章