django context data into javascript

260 views
Skip to first unread message

Soumen Khatua

unread,
Feb 25, 2020, 5:26:05 AM2/25/20
to django...@googlegroups.com
Hi Folks,

I'm sending some context data into html like 
name = 'soumen'
return render(request,'xyz.html', context = {'name':name})

inside myjs.js(js script has been integrated to html):
-------------------------------------------------------------------
var my_name = {{name}};

I'm getting an error::: Uncaught SyntaxError: Unexpected token '{'

So,How I can render data into frontend??

Thank You in advance

Gil Obradors

unread,
Feb 25, 2020, 5:59:59 AM2/25/20
to django...@googlegroups.com
Hi!

The context is a dictionary mapping template variable names to Python objects.

May be you need to test with a dict nor string directly.
I haven't test a direct variable.

Try not using JS, first resolve dict context in html , then JS.

Luck!

Missatge de Soumen Khatua <soumenk...@gmail.com> del dia dt., 25 de febr. 2020 a les 11:25:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WYM%2BvkoRqOaUn0%2Bt0j9fBf%2BhUZ2myvfsfY4qoBmLAgTfw%40mail.gmail.com.

Naveen Arora

unread,
Feb 25, 2020, 7:29:53 AM2/25/20
to Django users
Hi, this is simple just enclose {{name}} into " " as "{{name}}" for your part.

Thanks

Soumen Khatua

unread,
Feb 25, 2020, 12:31:19 PM2/25/20
to django...@googlegroups.com
But I'm getting one error

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Artem Vasin

unread,
Feb 25, 2020, 12:45:44 PM2/25/20
to django...@googlegroups.com
.js files, even if linked in Django templates are not processed by template engine. To output value from context to file, this file should be an output from template engine.

You have three ways to solve your problem:
  1. Make your myjs.js file generated by template processor. It’s most difficult and worst way so I don’t really suggest it, but feel free to experiment. Think about a view, which outputs js file content instead of html and link to this view url from your page template
  2. Move js assignment from myjs.js to your template (one in which you link myjs.js) wrap it in <script> tag and put BEFORE <script> tag which links to myjs.js
  3. Use json_script template filter in your template and rewrite myjs.js according to docs: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#json-script

I prefer to use second choice for simple values, like string or number and third for complex objects.

And while you at it, I advice you to wrap {{ name  }} in double or single quotes, to prevent js error when name is empty string and resulting js-code looks like var my_name = ; Also look at default and default_if_none template filters https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#default 

25 февр. 2020 г., в 20:30, Soumen Khatua <soumenk...@gmail.com> написал(а):

Reply all
Reply to author
Forward
0 new messages