gpt4 book ai didi

css - django 模板中的内联 CSS 文件

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:53 26 4
gpt4 key购买 nike

是否可以在 django 模板中内联首屏/关键 css 文件?

我在想这样的事情:

<style>{% inline "css/home.above.css" %}</style>

这会导致:

<style>html {...} body {...} {content of the css file}</style>

但是我还没有找到这方面的任何资源。

最佳答案

为了扩展我的评论,这是将 CSS 文件传递​​到模板上下文的方式。

def my_view(request):
with open('../path/to/style.css') as infile:
css = infile.read()
# if you want to remove newlines, uncomment the next line
# css = css.replace('\n', '')
# if you want to remove tabs, uncomment the next line
# css = css.replace('\t, '')

return render(request, 'template.html', {'css': css})

然后,在您的模板中,您可以使用 {{ css }} 访问整个 CSS 文件。


注意: 与其手动从 CSS 中删除换行符和制表符,我认为最好使用 CSS 压缩器。例如,如果您使用 4 个空格而不是制表符,那么这些额外的空格将不会被删除。

这个库看起来不错 - csscompressor .

关于css - django 模板中的内联 CSS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44207753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com