[英]Using innerHTML to include HTML Django page not work
I am trying to include an HTML when a button is clicked in a Django project but the error a Uncaught SyntaxError: Invalid or unexpected token
当在 Django 项目中单击按钮时,我试图包含一个 HTML 但错误是
Uncaught SyntaxError: Invalid or unexpected token
Here is the Javascript:这是 Javascript:
document.getElementById("workout_table").innerHTML = '{% include 'app/log_form.html' %}';
The error showing is at this line of code as if the include code is not there at all.错误显示在这行代码中,就好像根本不存在包含代码一样。
My question: How can I add this code through javascript and avoid this error我的问题:如何通过 javascript 添加此代码并避免此错误
Try changing quotation marks to Grave accent:尝试将引号更改为重音符:
document.getElementById("workout_table").innerHTML = `{% include 'app/log_form.html' %}`;
Try something like尝试类似的东西
document.getElementById("workout_table").innerHTML = {% json.dumps( include 'app/log_form.html') %};
Note the absence of quote marks around {%... %}
, which is based on the assumption that Python's json.dumps
method returns a string of characters starting and ending with ASCII double quote marks in the same way as JSON.stringify
does in JavaScript.请注意
{%... %}
周围没有引号,这是基于 Python 的json.dumps
方法返回一串以 ASCII 双引号开头和结尾的字符的假设,其方式与JSON.stringify
在JavaScript。
If the syntax of {% json.dumps( include 'app/log_form.html) %}
is incorrect, please amend it to cause the string content of file app.log_form.html
to be serialized as a JSON string and included in generated HTML.如果
{% json.dumps( include 'app/log_form.html) %}
语法不正确,请修改,使文件app.log_form.html
的字符串内容序列化为JSON字符串并包含在生成的HTML中.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.