Issue with passing arguments to button onClick js function with Django template
Question:
I am using django template as standalone.
When using it, the Django template rendering is escaping single-quotes (adding to ‘) for the arguments of onClick function call.
<button id="order" onclick="myFunction('{{ val }}')"> {{ val }} </button>
when rendered in python with with val = 'somevalue'
:
Output:
<button id="order" onclick="myFunction('somevalue')"> somevalue </button>
This results in error and the button click does not work.
Expected:
<button id="order" onclick="myFunction('somevalue')"> somevalue </button>
With more checking I found that the single-quotes inside double-quotes are escaped by django template rendering.
Is there any way this adding to single-quotes inside double-quotes can be avoided?
Answers:
Try this combination and let me know.
val = "'somestring'"
<button id="order" onclick="myFunction({{ val }})"> {{ val }} </button>
I figured out the issue after some debugging and stepping through the django rendering part.
The issue was, i was using
with open('template_file.html') as f:
htmlString = f.readlines()
t = Template('htmlString')`
c = Context({'val': 'someval'})
result_html = t.render(c)
With these steps, when the html file is read, it is converted into python string which will have escaped single quotes (‘)
This was the problem.
Solution
Change is to use django’s loader from django.template
template = loader.get_template('template_file.html')
ctxt = {'val':'somevalue'}
result_html = template.render(ctxt)
With this above, the output is as expected.
I am using django template as standalone.
When using it, the Django template rendering is escaping single-quotes (adding to ‘) for the arguments of onClick function call.
<button id="order" onclick="myFunction('{{ val }}')"> {{ val }} </button>
when rendered in python with with val = 'somevalue'
:
Output:
<button id="order" onclick="myFunction('somevalue')"> somevalue </button>
This results in error and the button click does not work.
Expected:
<button id="order" onclick="myFunction('somevalue')"> somevalue </button>
With more checking I found that the single-quotes inside double-quotes are escaped by django template rendering.
Is there any way this adding to single-quotes inside double-quotes can be avoided?
Try this combination and let me know.
val = "'somestring'"
<button id="order" onclick="myFunction({{ val }})"> {{ val }} </button>
I figured out the issue after some debugging and stepping through the django rendering part.
The issue was, i was using
with open('template_file.html') as f:
htmlString = f.readlines()
t = Template('htmlString')`
c = Context({'val': 'someval'})
result_html = t.render(c)
With these steps, when the html file is read, it is converted into python string which will have escaped single quotes (‘)
This was the problem.
Solution
Change is to use django’s loader from django.template
template = loader.get_template('template_file.html')
ctxt = {'val':'somevalue'}
result_html = template.render(ctxt)
With this above, the output is as expected.