Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to configure Gulp Livereload for Django?

I would like to use gulp-liveReload with Django, is it possible? I have read that there is an alternative for Grunt, but I would prefer work with Gulp, it is easier for me.

Thanks,

like image 730
JesusMurF Avatar asked Sep 28 '14 13:09

JesusMurF


3 Answers

I wrote up how to do it in a recent blog post here:

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

Basically you just need to write gulp tasks that watch what files you want to trigger livereloads, so for me that's templates:

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

But you could also trigger it on models.py, views.py, or anything else you want really.

like image 178
Frank Wiles Avatar answered Nov 11 '22 23:11

Frank Wiles


have you try this django-livereload ?

like image 23
eugene Avatar answered Nov 12 '22 01:11

eugene


I also was struggling to find an answer howto to start a django server and get the browser live reloading working at the same time, however it turned out that it is easy to achieve (even when working cross platform on windows and 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
    });
});
like image 1
Evgeny Bobkin Avatar answered Nov 12 '22 01:11

Evgeny Bobkin