【问题标题】:django-pipeline - Page load really slowdjango-pipeline - 页面加载真的很慢
【发布时间】:2015-03-30 03:14:21
【问题描述】:

我正在尝试使用 django-pipeline 来缩小静态资源,为它们使用缓存并使我的模板更简单。我的浏览器找到并加载了我的 CSS 和 JS 文件,但我的(非常简单的)主页加载大约需要 10 秒。

我正在使用 Python 2.7.6、Django 1.7.3 和 django-pipeline 1.4.3。 PyCharm 使用本地 virtualenv 运行开发服务器。

我的 settings.py 包含以下内容:

DEBUG = True
TEMPLATE_DEBUG = DEBUG

INSTALLED_APPS = (
    'django_admin_bootstrapped', # custom admin
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # pip installed apps
    'pipeline',
    # project apps
    'myapp',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'pipeline.middleware.MinifyHTMLMiddleware',
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'pipeline.finders.FileSystemFinder',
    'pipeline.finders.CachedFileFinder',
    'pipeline.finders.PipelineFinder',
)

STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'myapp/static'),
)

STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
PIPELINE_CSS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor'
PIPELINE_JS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor'

PIPELINE_CSS = {
    'base': {
        'source_filenames': (
            'myapp/css/base.css',
            'myapp/bower_components/bootstrap/dist/css/bootstrap.css',
            'myapp/bower_components/Hover/css/hover.css',
            'myapp/bower_components/font-awesome/css/font-awesome.css',
        ),
        'output_filename': 'css/myapp.css',
    },
}

PIPELINE_JS = {
    'base': {
        'source_filenames': (
            'myapp/bower_components/jquery/dist/jquery.min.js',
            'myapp/bower_components/bootstrap/dist/js/bootstrap.min.js',
        ),
        'output_filename': 'js/myapp.js',
    },
}

我的基本 HTML 模板包含以下内容:

{% load staticfiles %}
{% load pipeline %}

<!DOCTYPE html>
<html>
    <head>
        [...]
        {% block css %}
            {% stylesheet 'base' %}
        {% endblock css %}

        {% block javascript %}
            {% javascript 'base' %}
        {% endblock javascript %}

    </head>
    <body> [...] </body>
</html>

我的 home.html 扩展了 base.html 但不使用 css 或 javascript 管道的模板标签。

只是为了确保 yuglify 可用:

$ yuglify --version
0.1.4

我在这里做错了什么?

注意:如果PIPELINE_ENABLED = True,浏览器不会找到静态资源(myapp.css 和 myapp.js)。

【问题讨论】:

  • 运气好了吗?在 S3 本地使用管道时,我遇到了同样的问题。即,当我使用 DEBUG=True(和 PIPELINE_ENABLED=False,这是默认值)时会发生这种情况。启用管道后,它可以按预期工作。
  • 据我记得,页面加载速度非常慢,因为文件查找器需要通过一个巨大的路径树。问题是在开发中使用 debug=False,您必须使用特定的查找器:github.com/cyberdelia/django-pipeline/issues/418。我想我在创建这个主题时就到了这一点。因为我用 bower 管理前端包,所以所有目录(src、dist 等)和文件(.json 或其他无用文件)都包含在静态文件中!
  • 你碰巧在哪个域上运行?奇怪的是,当我使用dockerhost:8000 时,我遇到了类似的情况,当我改回localhost:8000 时,它就神秘地消失了。有时我想知道localhost 是否没有比仅作为主机文件中的默认条目更高的特权。

标签: python django django-staticfiles django-pipeline


【解决方案1】:

问题是模板标签代码正在做很多事情,包括当 debug 为 True 时为每个请求运行 collectstatic ,这使得 dev 非常缓慢。即使 debug 为 False,templatetag 仍将连接到 S3 并查询一些事情。当文件在本地时,这不是(大)问题,但在使用 S3 时却是。我能想到的唯一解决方案是编写我自己的简化模板标签 pipelines.py。

在开始之前,您需要了解两件重要的事情,首先要让管道工作我有一个空壳 S3PipelineStorage,它结合了管道和 boto,如果您有 s3 + 管道工作,您可能已经有了很重要:

from pipeline.storage import PipelineMixin
from storages.backends.s3boto import S3BotoStorage

class S3PipelineStorage(PipelineMixin, S3BotoStorage):
    pass

然后在设置中:

STATICFILES_STORAGE = 'path.to.your.file.S3PipelineStorage'

现在,如果您低头查看模板标签,您会看到我使用的staticfiles_storage.url 类似于原始模板标签。这会将 s3 路径添加到相对路径,但如果您不添加此设置,您将每次查询 S3 以生成 URL。您可以添加设置或只是硬编码您的 s3 路径而不是 staticfiles_storage.url,但我建议您添加设置,因为它会在为 s3 资源生成 URL 的任何地方提高性能。

AWS_S3_CUSTOM_DOMAIN = 'your_bucket-%s.s3.amazonaws.com' % ENVIRONMENT.lower()

现在您已准备好使用模板标签。要使用它只需 {% load pipelines %} 而不是 {% load pipeline %}。

from django.contrib.staticfiles.storage import staticfiles_storage
from django import template
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from pipeline.conf import settings

register = template.Library()

@register.simple_tag
def stylesheet(group):

    if group not in settings.PIPELINE_CSS:
        return ''

    if settings.DEBUG is False or settings.PIPELINE_ENABLED is True:
        context = {
            'type': 'text/css',
            'url': mark_safe(staticfiles_storage.url(settings.PIPELINE_CSS[group]['output_filename']))
        }
        html = render_to_string("pipeline/css.html", context)
    else:
        html = ''
        for path in settings.PIPELINE_CSS[group]['source_filenames']:
            context = {
                'type': 'text/css',
                'url': mark_safe(staticfiles_storage.url(path))
            }
            html = "%s\n        %s" % (html, render_to_string("pipeline/css.html", context))

    return html

@register.simple_tag
def javascript(group):

    if group not in settings.PIPELINE_JS:
        return ''

    if settings.DEBUG is False or settings.PIPELINE_ENABLED is True:
        context = {
            'type': 'text/javascript',
            'url': mark_safe(staticfiles_storage.url(settings.PIPELINE_JS[group]['output_filename']))
        }
        html = render_to_string("pipeline/js.html", context)
    else:
        html = ''
        for path in settings.PIPELINE_JS[group]['source_filenames']:
            context = {
                'type': 'text/javascript',
                'url': mark_safe(staticfiles_storage.url(path))
            }
            html = "%s\n        %s" % (html, render_to_string("pipeline/js.html", context))

    return html

【讨论】:

    猜你喜欢
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    相关资源
    最近更新 更多