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

我正在使用Python2.7.6、Django 1.7.3和django-管道1.4.3。PyCharm使用本地虚拟服务器运行开发服务器。
我的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我在这里做错什么了?
注意事项:浏览器找不到静态资产(myapp.css和myapp.js),如果是PIPELINE_ENABLED = True。
发布于 2015-10-20 21:47:26
问题在于,模板标记代码正在做大量工作,包括在调试为True时为每个请求运行收集器,这使开发速度慢得令人痛苦。即使调试是假的,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'现在,如果您向下看templatetag,您将看到我使用类似于原始templatetag的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 htmlhttps://stackoverflow.com/questions/28239383
复制相似问题