Django:动态生成 JS 是个好主意吗?

发布于 2024-10-04 02:29:58 字数 301 浏览 4 评论 0原文

当我为 Django 项目编写 JS 文件时,我当然会进行一些 AJAX 调用,并且目前这些调用的 url 是硬编码的(这非常难看)。

我正在考虑让 django (而不是 Apache)提供 JS 文件,这样我就可以利用模板标签({% url %} !!!)。

我有理由不应该这样做吗?

或者有正确的方法可以做到这一点吗?

(我至少可以给出一个:重新发送未更改的 JS 文件会消耗大量时间。如果有一个应用程序能够在重新启动 django 服务器时生成文件,并在之后静态地提供它们,那就太好了!)

When I write my JS files for a Django project, of course I do some AJAX calls, and for the moment the urls for those calls are hard-coded (which is very ugly).

I was thinking of having the JS files served by django (instead of Apache), so I could take advantage of the template tags ({% url %} !!!).

Is there a reason why I shouldn't do this ?

Or is there a right way to do this ?

(I can give a least one : it will consume a lot of time resending JS files that haven't changed. What would be great is to have an application that generates files when restarting django server, and serves them statically after !)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

掩耳倾听 2024-10-11 02:29:59

我在 djangopackages 中的资产管理应用程序中进行了更深入的搜索,发现 django-mediagenerator 提供了该功能,即使它没有很好的文档记录:您可以生成 js 或 css 文件作为 django 模板,然后静态地提供它们(它们也被捆绑在一起,并且缓存被管理等等......所以两只鸟一石+它真的很容易设置!)。

为了将 JS 文件生成为 django 模板(在设置 django-mediagenerator 后),只需

ROOT_MEDIA_FILTERS = {
    'js': 'mediagenerator.filters.template.Template',
}

在设置中添加过滤器 : 即可。

I searched deeper in those asset manager applications from djangopackages, have found out that django-mediagenerator provides that feature, even if it is not well documented : you can generate your js or css files as django templates, and then serve them statically (they are also bundled, and caching is managed etc ... so two birds with one stone + it is really easy to set-up !).

In order to have JS files generated as django templates (after having set-up django-mediagenerator), just add the filter :

ROOT_MEDIA_FILTERS = {
    'js': 'mediagenerator.filters.template.Template',
}

in your settings.

何止钟意 2024-10-11 02:29:59

在服务器上动态生成 Javascript 是一个非常强大的工具,我在我的项目中经历过它的优点和缺点。

一般来说,您希望尽可能保持静态,以最大程度地减少每个请求所需完成的工作。这包括尽可能多地使用浏览器缓存,这可能会成为您的情况的问题。

我通常做的是在我的基本模板的标题中添加一个块。在需要执行仅在运行时已知的自定义 JavaScript 的模板中(例如,基于登录用户的自定义),我将其添加到块中。在这里,我可以动态生成我知道不会被缓存的 JavaScript,因此我可以做出一些假设。缺点是更加复杂。

如果您需要的只是指向 url,或者有一些简单的配置等,那么我建议创建一个视图,该视图将返回具有这些设置的 Javascript 文件。您可以设置正确的标头(Etag、Cache-Control 等),以便浏览器将文件缓存一段合理的时间。当您升级代码时,请确保 Etag 会发生变化。

在需要使用配置的代码中,您需要始终检查您正在查找的变量是否已实际定义,否则当由于某种原因配置 JavaScript 时,您遇到难以调试的问题未正确加载。

Dynamically generating Javascript on your server can be a tremendously powerful tool and I've experienced both it's upside and downside in my projects.

In general you want to keep as much as possible static to minimize the work to be done on every request. This includes having the browser cache as much as possible, which might become a problem in your case.

What I usually do is to have a block in the header in my base template. In templates that need to do custom javascript that is only known at runtime (customization based on logged in user, for example), I add it to the block. Here I can dynamically generate javascript that I know won't be cached so I can make some assumptions. The downside is more complexity.

If what you need are just pointing to urls, or have some simple configuration, etc, then I would suggest creating a view that will return a Javascript file with these settings. You can set the correct headers(Etag, Cache-Control, etc) so the browser will cache the file for some reasonable time. When you upgrade your code, make sure the Etag will change.

In the code that needs to use the configuration, you need to always check that the variable you are looking for is actually defined otherwise you will run into problems that are hard to debug when for some reason the configuration javascript is not loaded correctly.

羁拥 2024-10-11 02:29:59

发送到浏览器的 .js 会有所不同。这可能会使调试变得更加麻烦。也许不是问题,但可能需要考虑......

The .js that gets sent to the browser would vary. That could make debugging more cumbersome. Maybe not a problem but something to potentially consider...

撧情箌佬 2024-10-11 02:29:59

如今,最好的方法是使用 Django.js

这是他们讨论 URL 反转的文档: http://djangojs.readthedocs.org/en/0.8.1/djangojs.html#reverse-urls

Nowadays, the best way to do this is to use Django.js

Here is the doc where they talk about the URL reversing: http://djangojs.readthedocs.org/en/0.8.1/djangojs.html#reverse-urls

吃素的狼 2024-10-11 02:29:58

我会选择混合技术。静态地提供大部分 javascript。但是在 Django 模板中,有一个

I would go for a hybrid technique. Serve most of your javascript statically. But in your Django template, have a <script> block that defines various global variables, which are generated by the server-side code - url is a good example. Then your static JS can refer to the variables that are generated in the dynamic code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文