[Answer]-Django/Ajax – Executing javascript in a template received through ajax call

1👍

If entry.Col1 contains string ‘my text’, resulting template will give you lines like this:

dCell.push(my text);

and, i suppose, you need

dCell.push('my text');

0👍

Make sure that this is executing after the DOM is loaded.

Try wrapping your script like:

window.onload=function(){
    //script here
}

You can see on jsfiddle that this should be working.

Update:

It seems to be a problem with how Django is inserting data since it works with hardcoded sample data. Try the escapejs template filter to handle any potential javascript escaping problems. I’ve had to debug similar problems before with newlines and ‘&’ symbols in particular.

dCell.push("{{ entry.Col1|escapejs }}");
👤j_syk

Leave a comment