简体   繁体   English

在Django中为devel和production生成不同的静态文件

[英]Serve different Static files on devel and production in Django

I have a production and local DJANGO development environment. 我有一个生产和当地的DJANGO开发环境。 To push things to production we have a deployer which minifies and gzips all CSS and JS files. 为了将产品推向生产,我们有一个部署程序,可以缩小和压缩所有CSS和JS文件。

To serve them on production I need to call them like 为了在生产中为他们服务,我需要称他们为

  <link rel="stylesheet" href="{{ STATIC_URL }}css/filename.min.css.gz">

However on development I want the normal css file served (that way I don't have to re-minify and gzip each time I save) with: 但是在开发时我希望提供正常的css文件(这样我每次保存时都不需要重新缩小和gzip):

  <link rel="stylesheet" href="{{ STATIC_URL }}css/filename.css">

Is there any way to achieve and automatize this behaviour by adding something to the deployer?, is there some other work-around (I could get rid of the .min extension if it's possible to add the .gz in a clean way? 有什么方法可以通过向部署者添加内容来实现和自动化这种行为吗?还有其他一些解决办法(如果可以以干净的方式添加.gz,我可以摆脱.min扩展吗?

I want to note the I know I could implement some html-parser which adds it on each deploy but I'm looking for a neat and django oriented solution. 我想要注意,我知道我可以实现一些html-parser,它会在每次部署时添加它,但我正在寻找一个整洁且面向django的解决方案。

As usual, there's a Django package for that! 像往常一样,有一个Django包! There are two I use: 我用两个:

django-compressor: http://django-compressor.readthedocs.org/en/latest/ django-pipeline: https://django-pipeline.readthedocs.org/en/latest/ django-compressor: http//django-compressor.readthedocs.org/en/latest/ django-pipeline: https//django-pipeline.readthedocs.org/en/latest/

I started on django-pipeline but have moved to using compressor as of late. 我开始使用django-pipeline,但最近才转向使用压缩器。 See the docs, I believe one will be what you're looking for. 看到文档,我相信一个将是你正在寻找的。 Good luck! 祝好运!

I like the @Nursultan idea. 我喜欢@Nursultan的想法。 To enforce this you could code a context processor like this: 要强制执行此操作,您可以像这样编写上下文处理器:

# On yourapp.context_processor.py
from django.conf import settings

def debug_set(request):
    return {'debug_set': settings.DEBUG}

# On your settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
    .
    .
    .
    'yourapp.context_processors.debug_set',
)

# On your templates
{% if debug_set %}
    <link rel="stylesheet" href="{{ STATIC_URL }}css/filename.css">
{% else %}
    <link rel="stylesheet" href="{{ STATIC_URL }}css/filename.min.css.gz">
{% endif %}

I have never met this problem, but I come up with these two solutions: 我从未遇到过这个问题,但我想出了这两个解决方案:

  1. Use different settings.py for production and development. 使用不同的settings.py进行生产和开发。 But it requires to have the same names for *.min.js and change minifier's configuration. 但它需要为*.min.js设置相同的名称并更改minifier的配置。
  2. Or use a global variable and write everywhere 或者使用全局变量并在任何地方写入

    {% if development_stage %} <link> {% else %} <link> {% endif %}

Django - How to make a variable available to all templates? Django - 如何使变量可用于所有模板?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM