0

I'm writing an app that needs dynamic javascript. I didn't know how to use Django template variable in javascript. when I searched I found some answeres like Django Template Variables and Javascript but I still have the problem. when I write this piece of code in my html :

 <script>jQuery(document).ready(function ($) {
 $(".nav").css({"opacity": "0.5"});
 $("#description").animate({opacity: '+=0.5'}, 10000);
 });
 </script>

every thing is fine. the navbar is transparent and <p id="description">{{description}}</p>is shown by jquery animate function. but when i change it to this:

 <script>jQuery(document).ready(function ($) {
 $(".nav").css({"opacity": "0.5"});
 var a = "{{description}}";
 $("#description").animate({opacity: '+=0.5'}, 10000);
 });
 </script>

the navbar is no more transparent and the description is no more shown. what is the problem ?

p.s : I changed a to a = "{{blah}}"; and there is no problem with it. the problem apears when It is a real template variable.

asked Nov 2, 2015 at 7:57
5
  • The javascript you've included is pretty much no different between code examples, You have an unused variable containing a string of description but other than that its the same Commented Nov 2, 2015 at 8:08
  • but when I define this unused variable the script does not work any more. that is my problem. Commented Nov 2, 2015 at 8:19
  • Are you using angular.js or something else that uses the same django syntax? adding unused variables shouldn't affect anything Commented Nov 2, 2015 at 8:21
  • i don't use Angular.js. I just use Jquery. Commented Nov 2, 2015 at 8:28
  • when I change a to a = "{{blah}}"; there is no problem. so it understand that it is a template variable but then it is failed to continue. i don't know why. Commented Nov 2, 2015 at 8:36

1 Answer 1

1

Use verbatim:

Stops the template engine from rendering the contents of this block tag.

A common use is to allow a JavaScript template layer that collides with Django’s syntax. For example:

{% verbatim %}
 {{if dying}}Still alive.{{/if}} 
{% endverbatim %}
answered Nov 2, 2015 at 8:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.