首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在javascript中使用django-comments表单?

如何在javascript中使用django-comments表单?
EN

Stack Overflow用户
提问于 2012-06-13 21:35:58
回答 1查看 549关注 0票数 0

我有一个笔记,比如system see image here:http://www.uploadscreenshot.com/image/1091218/5818195,当点击笔记时,你可以看到标题,消息和评论。我通过js文件发送它们,并将它们设置在视图中。我的问题是,我现在可以用django-comments表单做到这一点吗?如果我只是粘贴到模板中,并在引导弹出窗口中显示的div中使用{%表示notes中的notes %},它会显示每个不在该窗口上的所有表单(这是可以理解的)。

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

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

代码语言:javascript
复制
 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的相关部分:

代码语言:javascript
复制
 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")

我希望我说得够清楚了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-14 20:47:07

解决方案是通过视图发送CommentForm。

代码:模板

代码语言:javascript
复制
<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>

视图:

代码语言:javascript
复制
from django.contrib.comments.forms import CommentForm

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

js:

代码语言:javascript
复制
 $('form#form_comments div.kopce').html(note.form_html);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11016027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档