Tôi gặp sự cố khi sử dụng dal và cố gắng xây dựng một ví dụ đơn giản.django-autocomplete-light hướng dẫn: select2 không phải là lỗi chức năng
Tôi đã gần như chính xác giống như Django autocomplete light: field not populated và cũng áp dụng câu trả lời của liên kết.
Dưới đây là các bước tôi đã đi qua:
Bước 1) Khi tôi thêm như thế này trên tiêu đề của html (chỉ giống như câu trả lời ở trên):
<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>
Các lỗi của thanh tra trình duyệt console là như thế này:
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
Bước 2) Vì vậy, tôi đã thêm jquery.js và các file js khác trên thư viện dal:
<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>
Và lỗi thay đổi như thế này:
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
Bước 3) Và đối với người cuối cùng, tôi thấy hai plugin này và thêm vào và nó làm việc!
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>
Vì vậy, đây là những câu hỏi của tôi:
A) Tôi có nên thêm tất cả các plugin trên? Ví dụ: có hai select2.js trên các plugin của [bước 3]] và tôi bối rối và tự hỏi liệu chúng có hoạt động theo các cách khác nhau hay không. (Nếu tôi xóa một trong số chúng, nó không hoạt động.)
B) Theo câu hỏi Django autocomplete light: field not populated, không nên bước 2 làm việc? Tại sao nó xảy ra lỗi?
C) Trên mã mẫu này, phần mã sau dường như không hoạt động. Nếu không có tài khoản quản trị, khóa ngoại không xuất hiện trên trường văn bản, nói cách khác, không có gì có thể được tìm thấy trong danh sách. (Từ dal tutorial, nó cho phép 'Sử dụng tự động hoàn thành ngoài admin')
{% block footer %}
<script type="text/javascript" src="http://dal-yourlabs.rhcloud.com/static/collected/admin/js/vendor/jquery/jquery.js"></script>
{{ form.media }}
{% endblock %}
@ Tôi cũng kiểm tra django-autocomplete-light not working - select2 is not a function và sắp xếp lại các ứng dụng cài đặt đã không làm việc.