Javascript 使用ajax请求时在html中显示django语法时出现问题

Javascript 使用ajax请求时在html中显示django语法时出现问题,javascript,ajax,django,Javascript,Ajax,Django,我有一篇文章允许人们发表评论。 当用户单击提交的注释中的reply按钮时,将发送一个ajax请求,从文件中检索html,以向包含reply表单的DOM添加新元素 我正在使用以下代码: function handle_ajax_request(event) { if ( request.readyState === 4) { if (request.status === 200 || request.status === 304) { console

我有一篇文章允许人们发表评论。 当用户单击提交的注释中的reply按钮时,将发送一个ajax请求,从文件中检索html,以向包含reply表单的DOM添加新元素

我正在使用以下代码:

function handle_ajax_request(event) {
    if ( request.readyState === 4) {
        if (request.status === 200 || request.status === 304) {
            console.log("AJAX request from get_reply_form was received successfully from the server.");
            parentElement = document.getElementById("reply_comment");
            reply_form_element = document.createElement("div");
            reply_form_element.id = "reply_comment_form";
            parentElement.appendChild(reply_form_element);
            reply_form_element.innerHTML = request.responseText;
        } else {
            console.log("Error: AJAX request from get_reply_form failed!");
        }
    }
}

function get_reply_form(event) {
    event.preventDefault();
    reply = document.getElementById("reply_comment");

    request = new XMLHttpRequest();

    if (! request) {
        console.log("Error: Unable to create AJAX request.")
    }

    console.log("Created ajax request, checking for state.");
    request.onreadystatechange = handle_ajax_request;
    request.open("GET", "http://localhost:8000/static/html/experience/comment_reply_form.html", true);
    request.send(null)
}
我使用ajax请求获取此文件:

{% load comments %}
{% get_comment_form for article_details as form %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
  {{ form.object_pk }}
  {{ form.content_type }}
  {{ form.timestamp }}
  {{ form.security_hash }}

  {% if node.id %}
    <div class="form-group">
      <input type="hidden" class="form-control" name="parent" id="parent_id" value="{{ node.id }}" />
    </div>
  {% endif %}

  {{ form.comment }}
  <div class="col-md-4">
    <div class="form-group">
      <input type="hidden" class="form-control" name="next" value="/article/display/{{ article_details.id }}" />
      <input type="submit" class="form-control" value="Reply">
    </div>
  </div>
</form>
django语法是按字面意思显示的,如果django对其进行解释,则该语法根本不可见。为什么这不起作用

在html代码中,由于某种原因,在django语法之前插入了a,我不知道为什么,在最后添加了a。而且,似乎每次遇到a{时,a都放在前面,a放在末尾,我不知道为什么


我没有使用jquery,因为我目前正在学习javascript,我也喜欢学习普通版本,而不仅仅是框架。

您正在对静态HTML模板发出get请求。您应该对调用视图的URL发出请求,就像其他Django页面一样。

啊,这很有意义,所以Django知道它然后解析它,谢谢,我试试看。
Posted by an anonymous user 4 days, 20 hours ago
test
Reply
{% get_comment_form for article_details as form %}
{% csrf_token %} {{ form.object_pk }} {{ form.content_type }} {{ form.timestamp }} {{ form.security_hash }} {% if node.id %}
{% endif %} {{ form.comment }}