使用Jquery的DjangoAjax注释?
我想实现一个ajax评论系统 URL.py:使用Jquery的DjangoAjax注释?,django,jquery,django-templates,django-views,Django,Jquery,Django Templates,Django Views,我想实现一个ajax评论系统 URL.py: (r'^comment/(\d+)/$', comments), views.py: def comments(request,feed): if request.method == 'POST': feed=Feed.objects.get(pk=feed) form = CommentForm(request.POST) if form.is_valid(): comment, created = Co
(r'^comment/(\d+)/$', comments),
views.py:
def comments(request,feed):
if request.method == 'POST':
feed=Feed.objects.get(pk=feed)
form = CommentForm(request.POST)
if form.is_valid():
comment, created = Comment.objects.get_or_create(
feed=feed,
msg=form.cleaned_data['msg'],
ip=request.META['REMOTE_ADDR']
)
comments=Comment.objects.filter(feed=feed)
form=CommentForm()
variables=RequestContext(request,{'comments': comments,'feed': feed,'form':form,})
if 'HTTP_REFERER' in request.META:
return HttpResponseRedirect(request.META['HTTP_REFERER'])
return render_to_response('comment_page.html', variables )
#return HttpResponseRedirect('/view/')
else:
form=CommentForm()
feed=Feed.objects.get(pk=feed)
comments=Comment.objects.filter(feed=feed).reverse()
variables=RequestContext(request,{'comments': comments,'feed': feed,'form':form,})
return render_to_response('comment_page.html', variables )
模板:
<div id="commentbox" style="display:none;">
<form class="comment" method="post" action="/comment/{{feed.id}}/">
{{cform.as_p}}
<input class="post" type="submit" value="post" />
</form>
</div>
</br>
<h3></h3><button class="ccc">Show/Hide Comment</button> {{feed.comment_set.count}} Comments
<div id="commentlist" class="commentlist" style="padding-left:10px;"><ul style="list-style-type:square;">
{% for c in feed.comment_set.all %}
<li>{{c.msg}}</li>
{% endfor %}
</ul>
</div>
{{cform.as_p}}
显示/隐藏注释{feed.Comment\u set.count}}注释
{feed.comment_set.all%}
- {{c.msg}}
{%endfor%}
在不刷新页面的情况下,我应该包含哪些代码将注释添加到commentlist li字段中。我是阿贾克斯新手。请帮忙。谢谢以下是我要做的: 让HTML保持原样,因为它适用于没有JavaScript的人。在JavaScript中,当用户提交表单时,阻止它实际发生:
$('#commentbox form').submit(function(e) {
e.preventDefault();
});
现在,当按下按钮时,阻止默认行为并通过AJAX提交表单:
$('#commentbox form').submit(function(e) {
e.preventDefault();
$.ajax({
type: 'post',
url: $(this).parent().attr('action'),
data: $(this).parent().serialize(),
}).done(function(data) {
alert('The AJAX is done, and the server said ' + data);
});
});
当我将上述代码插入html时,“post”/“submit”按钮没有被按下。为什么会这样呢?那就去掉前两行。这是我亲手写的,所以测试已经完成了……你最好使用表单上的“提交”事件,而不是“点击”提交按钮。