1

ユーザーがデータベース ベースのスタイリング オプションを使用できるようにするには、非常に複雑なセットアップを考え出す必要がありました。ユーザーは、django admin バックエンドでスタイル (背景色、フォント フェイスなど) を入力します。

次のように、テンプレート ビューをプレーン テキスト ビューとしてレンダリングして、動的な LESS ファイルを作成しています。

ビュー.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は、このようなものです。ユーザーが管理者バックエンドで入力したデータベース スタイリング エントリを受け取ります。

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

ここで、この動的な less ファイルを他の通常の静的な LESS ファイルと共に main.less ファイルに含めます。そのようです:

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 はすべての less ファイルをまとめてコンパイルします。

LESS サーバー側をコンパイルして 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

そのようなジャンゴコンプレッサーの問題を経験した人はいますか? このような動的に作成されたファイルに問題はありますか? 絶対 URL が問題になる可能性はありますか?

djangoコンプレッサーで動作する動的に生成されるファイルを減らす別のソリューションを考えてもらえますか?

4

1 に答える 1

0

Django-Compressor は、URL にアクセスした場合に「オンザフライ」でしか利用できない、動的に作成された少ないファイルを読み取ることができないと思います。少なくとも私はそれを働かせませんでした。また、ファイルはCOMPRESS_ROOT.

モデルが保存されるたびに、少ないファイルを物理的にディスクに書き込みます。これがコードです。try except などの改善が必要ですが、動作します:

 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)
于 2012-10-13T09:01:44.967 に答える