【发布时间】:2016-07-26 08:59:27
【问题描述】:
我在使用dal 时遇到了问题,并尝试构建一个简单的示例。
我做的和Django autocomplete light: field not populated几乎一模一样,也应用了链接的答案。
这是我经历的步骤:
第 1 步)当我在 html 的标题上添加这样的内容时(与上面的答案相同):
<link href="http://dal-yourlabs.rhcloud.com/static/collected/autocomplete_light/vendor/select2/dist/css/select2.css" type="text/css" media="all" rel="stylesheet" />
<link href="http://dal-yourlabs.rhcloud.com/static/collected/autocomplete_light/select2.css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/autocomplete_light/select2.js"></script>
浏览器检查器控制台报错如下:
Uncaught ReferenceError: yl is not defined(anonymous function) @ autocomplete.init.js:75select2.js:117
Uncaught ReferenceError: yl is not defined(anonymous function) @ select2.js:117
第2步)所以我在dal库上添加了jquery.js和其他js文件:
<link href="http://dal-yourlabs.rhcloud.com/static/collected/autocomplete_light/vendor/select2/dist/css/select2.css" type="text/css" media="all" rel="stylesheet" />
<link href="http://dal-yourlabs.rhcloud.com/static/collected/autocomplete_light/select2.css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/admin/js/vendor/jquery/jquery.js"></script>
<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/autocomplete_light/jquery.init.js"></script>
<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/autocomplete_light/autocomplete.init.js"></script>
<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/autocomplete_light/select2.js"></script>
错误变成了这样:
select2.js:66 Uncaught TypeError: $(...).select2 is not a function(anonymous function) @ select2.js:66dispatch @ jquery.js:4435elemData.handle @ jquery.js:4121trigger @ jquery.js:4350(anonymous function) @ jquery.js:4901each @ jquery.js:374each @ jquery.js:139trigger @ jquery.js:4900initialize @ autocomplete.init.js:45each @ jquery.js:374each @ jquery.js:139(anonymous function) @ autocomplete.init.js:50fire @ jquery.js:3099fireWith @ jquery.js:3211ready @ jquery.js:3417completed @ jquery.js:3433
第 3 步)最后,我找到了这两个插件并添加了它,它起作用了!
Plugins of [Step 2)] +
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
所以这是我的问题:
A) 我应该添加上面的所有插件吗?例如,[步骤 3)] 的插件上有两个 select2.js,我很困惑,想知道它们是否以不同的方式工作。(如果我删除其中一个,它就不起作用。)
B)根据问题Django autocomplete light: field not populated,第2步不应该工作吗?为什么会出现错误?
C) 在此示例代码中,以下代码部分似乎根本不起作用。如果不是管理员帐户,则外键不会出现在文本字段中,换句话说,在列表中找不到任何内容。 (来自dal tutorial,它允许“在管理员之外使用自动完成”)
{% block footer %}
<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/admin/js/vendor/jquery/jquery.js"></script>
{{ form.media }}
{% endblock %}
@我还检查了django-autocomplete-light not working - select2 is not a function 并重新排列安装的应用程序不起作用。
【问题讨论】:
标签: django jquery-select2 django-autocomplete-light