0

I am using django for research and have an expensive computing process that takes about 12 or 13 hours and generates javascript charts.

I did the computing using the management command and stored the charts in the models as TextField.

My question is how to display this javascript code on the template?

I tried using {{ variable_name }} results in plain javascript text

I also tried using

<script type="javascript"> {{ variable_name }} </script>

resulted in text with special characters.

2 Answers 2

1

The rendering of code from django template must be done using the safe filter or all the "dangerous" code will be escaped.

Here the docs: https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#safe

Try it and check the html generated on the "View Source" facility on your browser. Check also the developer console to see any error if the javascript is correctly parsed

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer I was able to render the js store in my models using {{ variable_name|safe|escape }}. This solved my problem.
0

I'm not sure if the result is a valid javascript object, but basically what you want to do is put it inside a variable in javascript:

<script type="javascript">
    var my_obj = {{ data.word_graph }}
</script>

Note that it wil not work if the content of data.word_graph is not a valid javascript object (you can use json.dumps(python_obj) for example).

1 Comment

The result is a complete JS code <script type="javascript"> complete valid code, generated by mpld3 matplotlib </script> stored as a text field in the models

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.