Reputation: 45
Is their any way to define django variable in template by javascript variable
Upvotes: 1
Views: 110
Reputation: 31
I suppose you have to do few thing before delcare any django variable in temple by using javascript:
step 1: views.py
def XYZfunc(request, abc):
try:
some code here...
content = {'abc': abc}
return render(request, 'ProjectName/xyz_html_page.html', content)
except(ObjectDoesNotExist, KeyError, ValueError):
content = {'abc': abc}
return render(request, 'ProjectName/xyz_html_page.html', content)
step 2: urls.py (optional)
url(r'^ProjectName/xyz_htmlpage_name/(?P<abc>[A-Z]+)/$', views.XYZfunc, name="abc"),
(please note: if you are sending value through html url from views.py then use urls.py and if value is string then (?P[A-Z]+) )
Step 3: xyz_html_page.html
<script type="text/javascript">
var u = "{{abc}}";
</script>
i hope it might help you.
Upvotes: 0
Reputation: 510
you can do like this in your script.
<script type="text/javascript">
var a = "{{yourVariable}}";
</script>
and store it as script variable
Upvotes: 1