2014-09-28 83 views

回答

4

我寫了如何做到這一點在最近的一篇博客在這裏:

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

基本上你只需要編寫一個看你要觸發livereloads哪些文件一飲而盡任務,所以對我來說這是模板:

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

但是你也可以在models.py,views.py或其他任何你想要的東西上觸發它。

+0

我正在使用它,它可以很好地用於CSS文件更改等,但不適用於模板。使用模板的唯一方法是如果我觸摸或更改'.py'文件,然後更改模板。你遇到過這個問題,或者它以某種方式爲你工作? – 2015-11-20 00:50:03

+0

嗨邁克,道歉我在上面有一個錯字,應該是'**/templates/**',而不是我原來的'**/templates/*'。希望有所幫助! – 2015-11-21 13:24:33

1

你試試這個django-livereload

+0

我正在使用它與gulp,效果不錯 – eugene 2014-09-30 09:46:01

+0

不,我沒有嘗試過。最後我已經安裝了gulp-livereload並且它可以工作;)。謝謝。 – JesusMurF 2014-09-30 11:16:34

0

我假設你正在談論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 
})  
0

我試過Frank Wile's answer,但我是有它的問題。使用模板的唯一方法是我保存了.py文件,然後對模板進行了更改。

我延長弗蘭克的做法與基本touch調用livereload.changed()以前上課一個.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); 
    }); 
}); 
1

我也掙扎着找到答案HOWTO啓動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 
    }); 
}); 
+0

這對我來說非常有用。感謝發佈。 – Paul 2016-11-04 14:40:34

0

您必須包括:

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

to django main template。見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']); 
相關問題