Created
March 3, 2013 23:12
-
-
Save ferrouswheel/5078760 to your computer and use it in GitHub Desktop.
Set up Django with CSRF protection when using require.js, backbone, and backbone-relational.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
// Include this template in your base Django template. | |
// I also define my backbone API urls here, using Django's url resolver: | |
//var a_model_url = "{% url backbone:appname_modelname %}"; | |
var csrf_token = "{{ csrf_token }}"; | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require.config({ | |
baseUrl: "/static/js", | |
paths: { | |
"backbone": "libs/backbone/backbone-min", | |
"backbone-relational": "libs/backbone/backbone-relational", | |
"underscore": "libs/underscore/underscore", | |
"jquery": "libs/jquery/jquery-1.8.3", | |
}, | |
shim: { | |
'jquery-ui': { | |
deps: ['jquery'], | |
}, | |
'backbone-relational': { | |
deps: ['backbone'], | |
exports: 'Backbone' | |
}, | |
'backbone': { | |
deps: ['underscore', 'jquery'], | |
exports: 'Backbone', | |
init: function (underscore, jquery) { | |
var oldSync = this.Backbone.sync; | |
this.Backbone.sync = function(method, model, options) { | |
options.beforeSend = function(xhr){ | |
xhr.setRequestHeader('X-CSRFToken', window.csrf_token); | |
}; | |
return oldSync(method, model, options); | |
}; | |
} | |
}, | |
'underscore': { | |
exports: '_' | |
} | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment