具有动态LESS文件的Django压缩器会引发一个FilterError(err) [英] Django Compressor with dynamic LESS file raises a FilterError(err)

查看:195
本文介绍了具有动态LESS文件的Django压缩器会引发一个FilterError(err)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不提出相当复杂的设置,以便为用户启用基于数据库的样式选项。用户在django管理员后端输入样式(如背景颜色,字体等等)。

I had to come up with quite a complicated setup to enable database based styling options for users. Users enter styles (like background color, font face, etc...) in the django admin backend.

我正在通过将模板视图呈现为纯文本视图如下:

I am creating a dynamic LESS file by rendering a template view as plain text view like so:

views.py:

class PlainTextView(TemplateView):
    """
    Write customized settings into a special less file to overwrite the standard styling
    """
    template_name = 'custom_stylesheet.txt'

    def get_context_data(self, **kwargs):
        context = super(PlainTextView, self).get_context_data(**kwargs)
        try:
            #get the newest PlatformCustomizations dataset which is also activated
            platform_customizations = PlatformCustomizations.objects.filter(apply_customizations=True).order_by('-updated_at')[0]
        except IndexError:
            platform_customizations = ''

        context.update({
            'platform_customizations': platform_customizations,
        })
        return context


    def render_to_response(self, context):
        return super(PlainTextView, self).render_to_response(context, content_type='plain/text')

模板 custom_stylesheet.txt <强>看起来像这样。它需要在管理员后端中输入的用户的数据库样式条目:

The template custom_stylesheet.txt looks kind of like this. It takes the database styling entries the users entered in the admin backend:

@CIBaseColor: {{ dynamic_styles.ci_base_color }};
@CIBaseFont: {{ dynamic_styles.ci_base_font }};
...etc...

现在我将这个动态的少量文件无文件与其他正常的静态LESS文件。像这样:

Now I include this dynamic less files in my main.less file with other normal static LESS files. Like so:

main.less:

@import "bootstrap_variables.less";

//this is the dynamicly created custom stylesheet out of the dynamic_styles app
@import url(http://127.0.0.1:8000/dynamic_styles/custom_stylesheet.less);

//Other styles
@import "my_styles.less";

此设置工作正常。我的数据库中的动态变量被渲染到模板中,LESS将我所有的少量文件编译在一起。

This setup works fine. The dynamic variables out of my database get rendered into the template and LESS compiles all my less files together.

当我将代码推送到我的生产设置时,我有一个问题编译LESS服务器端并使用django-compression压缩。

I have a problem when pushing the code to my production setup where I compile the LESS server side and compress it with django-compressor.

我收到以下错误:

FilterError: [31mFileError: 'http://127.0.0.1:8000/dynamic_styles/custom_stylesheet.less' wasn't found.
[39m[31m in [39m/home/application/***/media/static-collected/styles/less/main.less[90m:13:0[39m
[90m12 //this is the dynamicly created custom stylesheet out of the dynamic_styles app[39m
13 [7m[31m[1m@[22mimport url(http://127.0.0.1:8000/dynamic_styles/custom_stylesheet.less);[39m[27m
[90m14 [39m[0m

有没有人遇到过这样的django压缩器的问题?这样动态创建的文件有问题吗?可能绝对的url是一个问题吗?

Has anybody ever experienced problems with django compressor like that? Does it have problems with dynamically created files like this? Could the absolute url be a problem?

你能想到另一个解决方案可以动态生成更少的文件使用django压缩器吗?

Could you think of another solution get dynamically generated less files working with django compressor?

推荐答案

我猜Django-Compressor无法读取动态创建的较少的文件,只有当您点击网址时,它们才可以即时使用。至少我没有得到它的工作。此外,文件需要位于 COMPRESS_ROOT

I guess Django-Compressor can't read dynamically created less files which are only available "on-the-fly" if you hit the url. At least I did not get it working. Also the file needs to be on the COMPRESS_ROOT.

现在,我每次写入少量文件到磁盘模型得到保存。这是代码。它仍然需要一些改进,例如除了等等,但它有效:

Now I write the less file to disk physically every time the model gets saved. Here's the code. It still needs some improvement like try except, etc. But it works:

 def save(self, *args, **kwargs):

    #todo add try except
    less_file = open(os.path.join(settings.PROJECT_ROOT, 'media', 'static', "styles", "less", "custom_stylesheet.less"), "w")
    less_file.write(render_to_string('template/custom_stylesheet.txt', {'platform_customizations': self}))
    less_file.close()

    super(PlatformCustomizations, self).save(*args, **kwargs)

这篇关于具有动态LESS文件的Django压缩器会引发一个FilterError(err)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆