Django模板呈现为自包含文件

时间:2022-09-04 20:19:14

I'm attempting to render a Django template into a self-contained HTML file. For instance, I would like CSS style sheets that are <link>-ed to be expanded into <style> sections. Same deal with Javascript files. This way the HTML file is entirely self-contained.

我正在尝试将Django模板呈现为一个自包含的HTML文件。例如,我希望将 -ed的CSS样式表扩展为

Is there an option in Django or an extension that will do this? If there isn't, what's the best approach for achieving this?

Django中有一个选项还是可以执行此操作的扩展名?如果没有,那么实现这一目标的最佳方法是什么?

1 个解决方案

#1


1  

Give a try to django-compressor. It's pretty easy to configure and use.

尝试django压缩机。配置和使用非常简单。

Quote from docs:

从文档引用:

The compress template tag supports a second argument specifying the output mode and defaults to saving the result in a file. Alternatively you can pass ‘inline‘ to the template tag to return the content directly to the rendered page, e.g.:

compress模板标记支持指定输出模式的第二个参数,默认为将结果保存在文件中。或者,您可以将“内联”传递给模板标记,以将内容直接返回到呈现的页面,例如:

{% load compress %}

{% compress js inline %}
<script src="/static/js/one.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">obj.value = "value";</script>
{% endcompress %}

would be rendered something like:

将呈现如下:

<script type="text/javascript" charset="utf-8">
obj = {};
obj.value = "value";
</script>

#1


1  

Give a try to django-compressor. It's pretty easy to configure and use.

尝试django压缩机。配置和使用非常简单。

Quote from docs:

从文档引用:

The compress template tag supports a second argument specifying the output mode and defaults to saving the result in a file. Alternatively you can pass ‘inline‘ to the template tag to return the content directly to the rendered page, e.g.:

compress模板标记支持指定输出模式的第二个参数,默认为将结果保存在文件中。或者,您可以将“内联”传递给模板标记,以将内容直接返回到呈现的页面,例如:

{% load compress %}

{% compress js inline %}
<script src="/static/js/one.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">obj.value = "value";</script>
{% endcompress %}

would be rendered something like:

将呈现如下:

<script type="text/javascript" charset="utf-8">
obj = {};
obj.value = "value";
</script>