Skip to content

Instantly share code, notes, and snippets.

@ferrouswheel
Created March 3, 2013 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferrouswheel/5078760 to your computer and use it in GitHub Desktop.
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.
<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>
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