javascript - 如何通过 javascript 使用 django 评论表单?

标签 javascript django comments django-comments

我有一个类似系统的注释,请参见此处的图片:http://www.uploadscreenshot.com/image/1091218/5818195单击注释时,您可以看到标题、消息和评论。我通过 js 文件发送它们并将它们设置在 View 中。 我的问题是现在我可以使用 django-comments 表单来做到这一点吗? 如果我只是粘贴到模板中,并在引导弹出窗口中显示的 div 内使用 {% for note in comments %} ,它会显示不在该窗口上的每个表单的所有表单(这是可以理解的)。

如何将正确的值传递到 django-comments 表单?

这是js函数(只是相关部分):

 request.done(function(note) {
    $('h3#view-note-title').text(note.title);
    $('p#view-note-desc').text(note.message);
    var html = '';
    for(var i=0; i<note.comments.length; i++) {
        var item = note.comments[i];
        html += "<p id='comments' style='display: block; background: #a3d95d;margin-bottom: 3px;'>" + item.comment + "</p>";
        html += "<p id='username' style='display: block;background: #edac65;margin-bottom: 3px;'>" + item.username + "</p>";
        html += "<p id='date' style='display: block;background: #afe9eb;margin-bottom: 13px;'>"+ item.submit_date +"</p>";
    }
    $('div#comments').html(html);
});

这是views.py的相关部分:

 if request.method == "GET" and request.is_ajax:
    note = get_object_or_404(Note, pk=request.GET['noteid'])
    ctype = ContentType.objects.get_for_model(Note)
    latest_comments = Comment.objects.filter(is_public=True, is_removed=False, content_type=ctype, object_pk=note.id).order_by('-submit_date')[:5]
    response_data = {}
    response_data['title'] = note.title
    response_data['message'] = note.message
    response_data['comments'] = [
        {'username': c.user.username, 'comment': c.comment, 'submit_date': c.submit_date} for c in latest_comments]
    return HttpResponse(json.dumps(response_data, cls=DjangoJSONEncoder), mimetype="application/json")

我希望我说得足够清楚。

最佳答案

解决方案是通过 View 发送 CommentForm。

代码: 模板

<form id="form_comments" action="{% comment_form_target %}" method="post">
{% csrf_token %}
<table>
<tr>
  <td colspan="2">
    <div class="kopce"></div>
    <input type="submit" name="submit" value="Post">
    <input type="submit" name="preview" value="Preview">
  </td>
</tr>

View :

from django.contrib.comments.forms import CommentForm

form = CommentForm(target_object = note)
response_data["form_html"] = form.as_p()

js:

 $('form#form_comments div.kopce').html(note.form_html);

关于javascript - 如何通过 javascript 使用 django 评论表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11016027/

相关文章:

javascript - CSS 复选框样式不起作用

javascript - jQuery - 从数组对象中获取值

python - 尝试将非有序查询集与多个有序值进行比较 django

editor - 带注释换行的文本编辑器

xcode - 如何更改 xcode 中快捷方式生成的注释样式?

javascript - 如何在不折叠其他列的情况下创建具有全宽内容的 Bootstrap 可折叠面板?

javascript - 如何使用 javascript 在 html 中截取屏幕截图?

使用 google 进行 Django openid 身份验证

django - 无法连接到 gmail smtp linode django apache2 安装程序

javascript - 清除表单并显示新评论 jquery php