[英]django-pipeline - Page load really slow
我正在嘗試使用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)。
問題是模板標記代碼正在做一堆事情,包括當debug為True時為每個請求運行collectstatic,這使得開發變得非常緩慢。 即使debug為False,templatetag仍會連接並查詢S3以獲取多項內容。 當文件是本地文件時,這不是一個(大)問題,但是當使用S3時它就是。 我能提出的唯一解決方案是編寫自己的簡化模板標簽,名為pipelines.py
。
在你進入它之前,你需要知道兩個重要的事情,首先是讓管道工作我有一個空的shell 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
你會看到我使用類似於原始模板staticfiles_storage.url
。 這會將s3路徑添加到相對路徑,但如果您不添加此設置,則每次都會查詢S3以生成URL。 您可以添加設置或只是硬編碼您的s3路徑而不是staticfiles_storage.url
但是我建議您添加設置,因為它將提高為s3資源生成URL的任何位置的性能。
AWS_S3_CUSTOM_DOMAIN = 'your_bucket-%s.s3.amazonaws.com' % ENVIRONMENT.lower()
現在您已准備好使用templatetag。 要使用它,只需{% 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.