简体   繁体   English

使用 innerHTML 包含 HTML Django 页面无效

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM