Access to django template variable from js block
<p>I have js code in my django template but project use Grunt js, so code must be in js block {% block extrajs %}.</p>

<p>This is my code in template:</p>

<pre><code>&lt;script type="text/javascript"&gt;
    var pub_date = {{ obj.pub_date|date:'YmdHi' }};
    var hour = moment().startOf('hour').fromNow();
    var time_ago = moment(pub_date, "YYYYMMDDhhmm").locale('{{ LANGUAGE_CODE }}').fromNow();
    document.write(time_ago);
&lt;/script&gt;
</code></pre>

<p>Here I try test my code but without success, I get empty alert window:</p>

<pre><code>$(document).ready(function(){
    var pub_date = '{{ obj.pub_date|date:'YmdHi' }}';
    alert(pub_date);
});
</code></pre>

<p>Question is, how I can get access from js block to variable in template?</p>

<p>In order to this work you have to have your js code inside your html template. Also when trying to do this add <code>|safe</code> behind your statement. For example :</p>

<pre><code>$(document).ready(function(){
    var pub_date = '{{ obj.pub_date|date:'YmdHi'|safe }}';
    alert(pub_date);
});
</code></pre>

javascript python django gruntjs django-templates