1

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

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

Sign up to request clarification or add additional context in comments.

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.

Comments

Your Answer

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.