Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp Task for Django runserver

I'm trying to create a task to start my Django backend.
It took me quite a while to solve these two challenges:

  1. Find the proper way to activate the virtualenv environment
  2. Get manage.py runserver to output to stdout

After searching for hours I have put together a solution.
I hope this will save someone a lot of time and frustration

like image 995
Zyphrax Avatar asked Jan 09 '23 15:01

Zyphrax


1 Answers

The gulp task below, written in coffeescript, will start the Django backend:

exec = require('child_process').exec

gulp.task 'serve:backend', ->
  proc = exec 'source bin/activate; PYTHONUNBUFFERED=1 ./manage.py runserver'
  proc.stderr.on 'data', (data) -> process.stdout.write data
  proc.stdout.on 'data', (data) -> process.stdout.write data

You can run the task with gulp serve:backend
Note:

  • You won't have to install any Node packages, child_process is built in.
  • Use exec instead of spawn to be able to run multiple commands in one call
  • Handle not only stdout but stderr as well or you won't see requests in your log
  • Don't forget PYTHONUNBUFFERED or you won't see anything in your console (grmbl)
like image 197
Zyphrax Avatar answered Jan 16 '23 22:01

Zyphrax