1

Is their any way to define django variable in template by javascript variable

jsanchezs
2,0783 gold badges30 silver badges56 bronze badges
asked May 16, 2018 at 9:47
1

2 Answers 2

1

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

0

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.