我有一个类似系统的注释,请参见此处的图片: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/