如何在django admin中更新jquery版本

时间:2022-07-02 20:08:34

How can I update jquery version in Django admin? Without replacing it in django/contrib/admin/media/js/admin/ jquery.min.js To newest jquery.min.js. Also I don't want to use two versions of jquery in my admin.

如何在Django admin中更新jquery版本?没有在django / contrib / admin / media / js / admin / jquery.min.js中替换它到最新的jquery.min.js。另外,我不想在我的管理员中使用两个版本的jquery。

2 个解决方案

#1


8  

That's your only two options. You're right about changing the actual Django file being a bad idea, so the only other option is to load your own in addition to that.

这是你唯一的两个选择。你改变实际的Django文件是个坏主意是正确的,所以唯一的另一个选择就是加载你自己的文件。

The Django version of jQuery is already namespaced as django.jQuery, so you can load your own version of jQuery with or without jQuery.noConflict().

Django版本的jQuery已经命名为django.jQuery,因此您可以使用或不使用jQuery.noConflict()加载您自己的jQuery版本。

I understand you feelings toward running two versions of jQuery on the same page, but this is the admin site, not your user-facing website. It's not that much of a speed cost loading the library twice, but it will only even be applicable to the handle of users (or maybe just yourself) who use the admin.

我理解你在同一页面上运行两个版本的jQuery的感觉,但这是管理员网站,而不是你面向用户的网站。加载库两次的速度成本并不是很高,但它甚至只适用于使用管理员的用户(或者只是你自己)的句柄。

#2


3  

I had the same problem, and I found there are few ways to achieve it. Since django admin base template does not directly include jQuery JS (hardcoded in template) and relies on admin framework to supply it, I cannot override it in template. So, the other possible ways are -

我遇到了同样的问题,我发现实现它的方法很少。由于django管理员基础模板不直接包含jQuery JS(在模板中硬编码)并依赖于管理框架来提供它,我无法在模板中覆盖它。所以,其他可能的方法是 -

  1. With Django's new staticfiles app, you can apply command collectstatic to get all the static files in the app (including django.contrib.admin). You can manually override the content here. It would be better to overwrite a latest jquery-min version since django admin templates include the uncompressed jquery.

    使用Django的新静态文件应用程序,您可以应用命令collectstatic来获取应用程序中的所有静态文件(包括django.contrib.admin)。您可以在此处手动覆盖内容。由于django管理模板包含未压缩的jquery,因此最好覆盖最新的jquery-min版本。

  2. You can use the ReWrite rule on deployed server to always point the jquery.js to some other url like jquery.latest.min.js where you keep a single copy of latest minified jquery. I would prefer this option.

    您可以在已部署的服务器上使用ReWrite规则始终将jquery.js指向其他URL,例如jquery.latest.min.js,其中保留最新缩小的jquery的单个副本。我更喜欢这个选项。

Rewrite rules are defined here :

重写规则在此处定义:

  1. Apache : http://httpd.apache.org/docs/current/mod/mod_rewrite.html
  2. Apache:http://httpd.apache.org/docs/current/mod/mod_rewrite.html
  3. Nginx : http://wiki.nginx.org/HttpRewriteModule
  4. Nginx:http://wiki.nginx.org/HttpRewriteModule

#1


8  

That's your only two options. You're right about changing the actual Django file being a bad idea, so the only other option is to load your own in addition to that.

这是你唯一的两个选择。你改变实际的Django文件是个坏主意是正确的,所以唯一的另一个选择就是加载你自己的文件。

The Django version of jQuery is already namespaced as django.jQuery, so you can load your own version of jQuery with or without jQuery.noConflict().

Django版本的jQuery已经命名为django.jQuery,因此您可以使用或不使用jQuery.noConflict()加载您自己的jQuery版本。

I understand you feelings toward running two versions of jQuery on the same page, but this is the admin site, not your user-facing website. It's not that much of a speed cost loading the library twice, but it will only even be applicable to the handle of users (or maybe just yourself) who use the admin.

我理解你在同一页面上运行两个版本的jQuery的感觉,但这是管理员网站,而不是你面向用户的网站。加载库两次的速度成本并不是很高,但它甚至只适用于使用管理员的用户(或者只是你自己)的句柄。

#2


3  

I had the same problem, and I found there are few ways to achieve it. Since django admin base template does not directly include jQuery JS (hardcoded in template) and relies on admin framework to supply it, I cannot override it in template. So, the other possible ways are -

我遇到了同样的问题,我发现实现它的方法很少。由于django管理员基础模板不直接包含jQuery JS(在模板中硬编码)并依赖于管理框架来提供它,我无法在模板中覆盖它。所以,其他可能的方法是 -

  1. With Django's new staticfiles app, you can apply command collectstatic to get all the static files in the app (including django.contrib.admin). You can manually override the content here. It would be better to overwrite a latest jquery-min version since django admin templates include the uncompressed jquery.

    使用Django的新静态文件应用程序,您可以应用命令collectstatic来获取应用程序中的所有静态文件(包括django.contrib.admin)。您可以在此处手动覆盖内容。由于django管理模板包含未压缩的jquery,因此最好覆盖最新的jquery-min版本。

  2. You can use the ReWrite rule on deployed server to always point the jquery.js to some other url like jquery.latest.min.js where you keep a single copy of latest minified jquery. I would prefer this option.

    您可以在已部署的服务器上使用ReWrite规则始终将jquery.js指向其他URL,例如jquery.latest.min.js,其中保留最新缩小的jquery的单个副本。我更喜欢这个选项。

Rewrite rules are defined here :

重写规则在此处定义:

  1. Apache : http://httpd.apache.org/docs/current/mod/mod_rewrite.html
  2. Apache:http://httpd.apache.org/docs/current/mod/mod_rewrite.html
  3. Nginx : http://wiki.nginx.org/HttpRewriteModule
  4. Nginx:http://wiki.nginx.org/HttpRewriteModule