有可能为Django配置Gulp Livereload吗



我想在Django中使用gulp-liveReload,有可能吗?我读到Grunt有一个替代品,但我更喜欢与Gulp合作,这对我来说更容易。

谢谢,

我在最近的一篇博客文章中写下了如何做到这一点:

http://www.revsys.com/blog/2014/oct/21/ultimate-front-end-development-setup/

基本上,你只需要编写gullow任务,观察你想触发livereloads的文件,所以对我来说,这就是模板:

/* Trigger a live reload on any Django template changes */
gulp.watch('**/templates/**').on('change', livereload.changed);

但你也可以在models.py、views.py或任何你真正想要的东西上触发它。

你试过这个django livereload吗?

我也很难找到如何启动django服务器并同时让浏览器实时重新加载的答案,但事实证明这很容易实现(即使在windows和linux上跨平台工作):

//jshint node:true
'use strict';
var gulp          = require('gulp'),
    ....
    gutil         = require('gulp-util');
var browserSync = require('browser-sync').create();
var serverUrl = 'localhost:8000';
var exec = require('child_process').exec;
var isProd = gutil.env.type === 'prod';
....
gulp.task('sass', function() {
  return sass(sources.sass, {
    compass: true,
    sourcemap: true,
    style: isProd ? 'compressed': 'expanded',
  })
  ....
  .pipe(sourcemaps.write('.'))
  .pipe(gulp.dest(targets.css))
  .pipe(browserSync.stream());
});
....
gulp.task('watch', function() {
  gulp.watch(sources.sass, ['sass']);
  gulp.watch(sources.pug, ['html'])
    .on('change', browserSync.reload);
  gulp.watch(sources.ts, ['typescript'])
    .on('change', browserSync.reload);
  // update browser on python change
  gulp.watch('website/**/*.py')
    .on('change', browserSync.reload);
});

// start django server
gulp.task('webserver', function() {
  var isWin = /^win/.test(process.platform);
  var cmd =  '. venv/bin/activate && PYTHONUNBUFFERED=1 ';
  if (isWin) { //for Windows
    cmd = 'venv\Scripts\activate.bat && set PYTHONUNBUFFERED=1 && ';
  }
  var proc = exec(cmd + 'python manage.py runserver ' + serverUrl);
  proc.stderr.on('data', function(data) {
    process.stdout.write(data);
  });
  proc.stdout.on('data', function(data) {
    process.stdout.write(data);
  });
});

// start livereload
gulp.task('browser-sync', ['typescript', 'html', 'sass', 'webserver'], function() {
    browserSync.init({
      proxy: serverUrl,
      port: 8080,
      reloadDelay: 300,
      reloadDebounce: 500
    });
});

我假设您谈论的是django项目中的静态资产(static下的东西)。

关键步骤是让python manage.py runserver服务的页面知道livereload server(通常由tiny-lr实现)的变化

一个简单的方法是将以下代码注入base.html(假设它是django项目中其余html模板的父模板)

# views.py
from django.conf import settings
# other imports ...
def index(request):
    ctx = {'debug': settings.DEBUG, }
    # other context setup...
    return render(request, 'index.html', ctx)
# other views' definitions...
<!-- base.html -->
{% if debug %}
    <!-- 
         assuming your livereload server runs at localhost:35729 
                                         (default host:port for livereload )
    -->
    <script src="//localhost:35729/livereload.js"></script>
{% endif %}
// gulpfile.js
var gulp = require('gulp'),
    gutil = require('gulp-util'),
    coffee = require('gulp-coffee'),
    livereload = require('gulp-livereload'),
    debug = require('gulp-debug'),
    watch = require('gulp-watch');
// other tasks definitions...
gulp.task('watch', function(){
    livereload.listen();    // starts a tiny-lr server, 
                            // default livereload server port is 35729
    gulp.src('static/src/**/*.coffee')
        .pipe(watch('static/src/**/*.coffee'))
        .pipe(coffee())
        .pipe(gulp.dest('static/dist'))
        .pipe(livereload()); // send a signal to tiny-lr server 
                             // to make the page with livereload.js 
                             // to perform a reload action
})    

我尝试了Frank Wile的答案,但我遇到了问题。对我来说,使用模板的唯一方法是保存一个.py文件,然后对模板进行更改。

在调用livereload.changed():之前,我用一个函数扩展了Frank的方法,该函数本质上是touch是一个.py文件

function touchPy() {
    gulp.src(appName + '/__init__.py')
        .pipe(gulp.dest(appName));
}
gulp.task('watch', function() {
    livereload.listen();
    // Changes to .css files works fine w/ Frank's approach
    gulp.watch(paths.static).on('change', livereload.changed);
    // Changes to .html files don't trigger a runserver refresh w/o touching a .py file 
    gulp.watch(paths.templates).on('change', function(file) {
        touchPy();
        livereload.changed(file);
    });
});

您必须包括:

<script type="text/javascript" src="http://127.0.0.1:32700/livereload.js" />

到django主模板。看见http://argskwargs.io/blog/javascript/django-typescript-gulp-part-three/。这是js和css文件的示例:

var gulp = require('gulp');
var watch = require('gulp-watch');
var livereload = require('gulp-livereload');
var gzip_options = {
    threshold: '1kb',
    gzipOptions: {
        level: 9
    }
};
gulp.task('css', function() {
    return gulp.src('static/css/*.css')
        .pipe(livereload());
});
gulp.task('js', function() {
    return gulp.src('static/js/*.js')
        .pipe(livereload());
});
/* Watch Files For Changes */
gulp.task('watch', function() {
    livereload.listen(32700);
    gulp.watch('static/css/*.css', ['css']);
    gulp.watch('static/js/*.js', ['js']);
    gulp.watch('static/css/*', 'static/js/*').on('change', livereload.changed);
});
gulp.task('default', ['css', 'js', 'watch']);

最新更新