5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python - Django css・jsをコンプレス

Posted at

django-css

css・jsをコンプレスしてくれます。ページアクセス時にキャッシュを生成している。
settings.pyでDEBUG = Trueを指定している場合は圧縮しません。

1 pipでinstallする

    $ pip install django-css

2 settings.pyを編集する

    # MEDIA_ROOT、MEDIA_URLは適切に設定されていることを確認
    MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'www')
    MEDIA_URL = '/www/'

    INSTALLED_APPS = (
        # 'compressor'を追記する
        'compressor',
    )
  1. テンプレートでの使い方
    {% load compress %}
    {% compress css %}
    <link rel="stylesheet" href="/media/css/one.css" type="text/css" charset="utf-8">
    <link rel="stylesheet" href="/media/css/two.sass" type="text/css" charset="utf-8">
    {% endcompress %}

    {% load compress %}
    {% compress js %}
    <script src="/media/js/one.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" charset="utf-8">obj.value = "value";</script>
    {% endcompress %}
  1. ページアクセス時のソースコードで圧縮されていることを確認
    <link rel="stylesheet" href="/media/CACHE/css/f7c661b7a124.css" type="text/css" media="all" charset="utf-8">
    <script type="text/javascript" src="/media/CACHE/js/3f33b9146e12.js" charset="utf-8"></script>

参考文献

Package Index django-css 2.3.1

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?