Also, use Django templating for the dynamic generated JS code and use more idiomatic coding techniques. Thanks Matthew Tretter for the report and the patch.