django - how to pass template variables to javascript -
friends, trying pass template variable javascript in order create chart. not getting it. django considers js files static files. dynamic template variables aren't available javascripts.
i tried this:
passing template variables javascript
but didn't either. 1 variable passed. rest didn't.
def profile(request,username): try: u = user.objects.get(username=username) questions = question.objects.filter(user=u) answers = answer.objects.filter(user=u) docs = document.objects.filter(user=u) except user.doesnotexist: pass return render(request,"auths/profile.html",locals())
also, described in above link, profile.html is
<input type="hidden" value="{{ u.username }}" id="username" /> <input type="hidden" value="{{ docs.count }}" id="docs" /> <input type="hidden" value="{{ questions.count }}" id="questions" /> <input type="hidden" value="{{ answers.count }}" id="answers" /> {% block %} <script type="text/javascript"> var user = document.getelementbyid("username").value; var docs = document.getelementbyid("docs").value; var questions = document.getelementbyid("questions").value; var answers = document.getelementbyid("answers").value; </script> <script type="text/javascript" src="{% static 'highcharts.js'%}"> </script> <script type="text/javascript" src="{% static 'highcharts-3d.js'%}"></script> <script type="text/javascript" src="{% static 'chart.js' %}"></script> {% endblock %}
and section of chart.js pass data :
series: [{ type: 'column', name: 'user profile', data: [ ['questions', questions], ['answers', answers], ['documents', docs] ] }]
as explained earlier, problem 1 variable gets passed while rest don't. result, no chart rendered. should friends?
in case if helps, using django 1.9 , highcharts.
to answer own question, think figured out wrong. chart values in number format. passing string. needed done convert strings values or number using javascript typecasting.
and section of chart.js pass data :
series: [{ type: 'column', name: 'user profile', data: [ ['questions', number(questions)], ['answers', number(answers)], ['documents', number(docs)] ] }]
changing strings numbers, chart rendered!
Comments
Post a Comment