Is their any way to define django variable in template by javascript variable
jsanchezs
2,0783 gold badges30 silver badges56 bronze badges
-
Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please learn how to create a Minimal, Complete, and Verifiable Example.user8060120– user80601202018年05月16日 10:04:28 +00:00Commented May 16, 2018 at 10:04
2 Answers 2
you can do like this in your script.
<script type="text/javascript">
var a = "{{yourVariable}}";
</script>
and store it as script variable
answered May 16, 2018 at 9:52
Comments
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.
answered May 16, 2018 at 10:44
Comments
default