0

我已经在我的项目中安装了 django-contib-comments 并且我有一个 HTML 来显示评论列表并显示输入新的表单。

我现在想使用 Ajax 在不刷新页面的情况下提交表单,并成功将提交的评论添加到列表中。

我已经完成了大部分工作,但我确信必须有一种更简单的方法来实现这一点。

我的问题是我是否有办法在 javascript 中呈现 Django HTML 标记,如下所示:

 document.getElementById("comments").innerHTML = {% render_comment_list for obj %}

到目前为止,这是我所做的代码:

1)我不想更改 django-contrib-comments 项目中的任何内容(我避免覆盖方法。

2) 我使用 django-contrib-comments 中的标准标签来呈现评论列表。

   {% render_comment_list for obj %}

3) 创建一个处理表单提交的 JavaScript,然后在列表中创建一个新条目。

function submit_comments(event) {
    event.stopPropagation();
    $.ajax({
        type: $('#comment_form').attr('method'),
        url: $('#comment_form').attr('action'),
        data: $('#comment_form').serialize(),
        cache: false,
        dataType: "html",
        success: function (html, textStatus) {
            var comment_count_btn = document.getElementById('comment-text-vertical-btn');
            if (comment_count_btn != null) {
                if (!isNaN(parseInt(comment_count_btn.innerHTML))) {
                    comment_count_btn.innerHTML = parseInt(comment_count_btn.innerHTML) + 1 + " Comments";
                }
            }
            var comment_count_head = document.getElementById('kapua-comments-header');
            if (comment_count_head != null) {
                if (!isNaN(parseInt(comment_count_head.innerHTML))) {
                    comment_count_head.innerHTML = parseInt(comment_count_head.innerHTML) + 1 + " Comments:";
                }
            }

            if (document.getElementById("comments") != null){
                submitted_timestamp = getQueryParameter("timestamp", this.data);
                submitted_date = new Date();
                if (submitted_timestamp == null) {
                    submitted_date = new Date(submitted_timestamp);
                }

                submitted_comment = getQueryParameter("comment", this.data);
                if (submitted_comment == null) {
                    submitted_comment = "No value entered"
                }

                html_comment = "<div class=\"right-aligned\"><div class=\"comment-date\">" + submitted_date + " - " + "</div>" + submitted_comment + "</div><br><br>";
                current_html = document.getElementById("comments").innerHTML;
                document.getElementById("comments").innerHTML = html_comment + current_html;
            }

            if (document.getElementById("comment_form") != null){
                document.getElementById("comment_form").reset();
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            $('#comment_form').replaceWith('Your comment was unable to be posted at this time.  We apologise for the inconvenience.');
        }
    });
    return false;
}; 

提前致谢

4

1 回答 1

0

是的,在 django 中有方法,您可以使用它[escape][1]来呈现 HTML 标签。这是一个简单的例子 -

from django.utils.html import escape
def example(request):
    return "%s<div><i>%s</i></div>" % (escape(text), escape(other_text)

更多解释可以参考文档。

https://docs.djangoproject.com/en/2.1/ref/utils/#django.utils.html.escape

还是有什么疑惑评论出来。

于 2018-10-10T15:08:52.717 回答