Search code examples
djangogulp

gulp and running django server with logging


I'm trying to run a django server from gulp. Although I've now managed (roughly) to activate the virtual env and then start the server, none of the output/errors from the django server appears. How can I get this to appear when using gulp?

gulpfile.js

var pjson = require('./package.json');

// Run django server
var cmd =  'workon switcher5'
gulp.task('runServer', function() {
  exec('~/virtualenvs/' + pjson.name + '/bin/python manage.py runserver', function (err, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
  });
});

Note although the server runs fine. I am getting the error in the console:

/bin/sh: /Users/User/virtualenvs/switcher5/bin/python: No such file or directory

(Running the django server not via gulp works as normal. )

I tried to spawn a process instead:

gulp.task('runServer', function(){
  console.info('starting django server');
  var PIPE = {stdio: 'inherit'};
  spawn('~/virtualenvs/' + pjson.name + '/bin/python', ['manage.py', 'runserver'], PIPE);
});

However although that ran with spawn('python', ['manage.py', 'runserver'], PIPE);from the terminal (activating virtualenv before manually running via the above fails. I'm not clear what's wrong with the path (if anything)

When it ran from the terminal it now gives output, but seems to be only errors from the django server (304's etc...), but no successful requests appear.


Solution

  • The key issue in the question was to get gulp to launch a virtualenv then launch the django server while passing the output from it to node via stdout.

    The other answer mention unfortunately doesn't achieve this as it doesn't launch a virtualenv at the same time.
    The solution below, does work. (note if you don't use virtualenv in a venv/ folder then just adjust the below to the relevant location

    // Activates a virtualenv and runs django server
    gulp.task('runServer', function(cb) {
      var cmd = spawn('venv/bin/python', ['manage.py', 'runserver'], {stdio: 'inherit'});
      cmd.on('close', function(code) {
        console.log('runServer exited with code ' + code);
        cb(code);
      });
    });