Django-Autocomplete-Light Widget导致Django-Rest-Framework中的加载时间过长

时间:2022-03-18 01:39:42

I'm currently working on an API using Django-Rest-Framework. The API itself is working fine and I'm now trying to implement Filtering for the Resources. This is working like a charm as well, the problem is though, that as I try to use the ModelSelect2-Widget from Django-Autocomplete-Light in order to handle the filtering for relationships in the Browsable API, (as there can be a lot of possible options to choose from, I'm using the autocomplete-widget to dynamically load more options if needed) the browser suddenly takes way too long to load the page due to scripts. (Firefox actually asks whether it should shut down jQuery)

我目前正在使用Django-Rest-Framework开发API。 API本身工作正常,我现在正在尝试为资源实现过滤。这也像魅力一样,问题是,当我尝试使用Django-Autocomplete-Light中的ModelSelect2-Widget来处理Browsable API中关系的过滤时,(因为可能会有很多)可供选择的选项,我正在使用自动完成小部件来动态加载更多选项,如果需要的话)由于脚本,浏览器突然花费太长时间来加载页面。 (Firefox实际上会询问它是否应该关闭jQuery)

I managed to narrow the problem down to some sort of problem between the scripts that DRF uses and those which DAL needs.

我设法将问题缩小到DRF使用的脚本和DAL需要的脚本之间的某种问题。

These are the scripts that are loaded with the page by Django Rest Framework (Source):

这些是由Django Rest Framework(Source)加载页面的脚本:

  <script>
    window.drf = {
      csrfCookieName: "{{ csrf_cookie_name|default:'csrftoken' }}"
    };
  </script>
  <script src="{% static "rest_framework/js/jquery-1.12.4.min.js" %}"></script>
  <script src="{% static "rest_framework/js/ajax-form.js" %}"></script>
  <script src="{% static "rest_framework/js/csrf.js" %}"></script>
  <script src="{% static "rest_framework/js/bootstrap.min.js" %}"></script>
  <script src="{% static "rest_framework/js/prettify-min.js" %}"></script>
  <script src="{% static "rest_framework/js/default.js" %}"></script>
  <script>
    $(document).ready(function() {
      $('form').ajaxForm();
    });
  </script>

And by Django-Autocomplete-Light (Source for autocomplet.init.js and jquery.init.js and Source for select2.js and select2.full.js):

并通过Django-Autocomplete-Light(autocomplet.init.js和jquery.init.js的源代码以及select2.js和select2.full.js的Source):

<script src="{% static 'autocomplete_light/jquery.init.js' %}"></script>
<script src="{% static 'autocomplete_light/autocomplete.init.js' %}"></script>
<script src="{% static 'autocomplete_light/vendor/select2/dist/js/select2.full.js' %}"></script>
<script src="{% static 'autocomplete_light/select2.js' %}"></script>

The problem seems to be connected to prettify-min.js and the Scripts from DAL, as I can comment out one of them and the page works again just fine.

这个问题似乎与来自美国国家标准化组织的美化时的mintify-min.js和Scripts有关,因为我可以将其中一个评论出来并且页面再次正常工作。

Screenshot from Firefox's Runtime Analysis: Django-Autocomplete-Light Widget导致Django-Rest-Framework中的加载时间过长 It gets worse the more data needs to be "prettified" by the script.

Firefox的运行时分析的屏幕截图:脚本需要“修饰”的数据越多,情况就越糟糕。

Does anyone see how to solve this?

有谁看到如何解决这个问题?

1 个解决方案

#1


0  

So I somehow solved the problem myself... I simply put the contents of autocomplete.init.js and select2.js in $(document).ready(function(){...}). Why? I don't know, but it seems to work just fine. ¯\_(ツ)_/¯

所以我自己以某种方式解决了这个问题...我只是在$(document).ready(function(){...})中输入autocomplete.init.js和select2.js的内容。为什么?我不知道,但似乎工作得很好。 ¯\ _(ツ)_ /¯

If someone knows the explanation for that, please feel free to share it.

如果有人知道这方面的解释,请随时分享。

#1


0  

So I somehow solved the problem myself... I simply put the contents of autocomplete.init.js and select2.js in $(document).ready(function(){...}). Why? I don't know, but it seems to work just fine. ¯\_(ツ)_/¯

所以我自己以某种方式解决了这个问题...我只是在$(document).ready(function(){...})中输入autocomplete.init.js和select2.js的内容。为什么?我不知道,但似乎工作得很好。 ¯\ _(ツ)_ /¯

If someone knows the explanation for that, please feel free to share it.

如果有人知道这方面的解释,请随时分享。