我正在做的是跟随。 post_detail.html
<div class="comment_form">
<form id="comment_form" method="POST" class="post-form">
{% csrf_token %}
{% include "myblog/post_comment.html" %}
<button id="add_comment" type="submit" class="save btn btn-default" >Comment</button>
</form>
</div>
发布详情视图
def post_detail(request, slug):
post = get_object_or_404(Post, slug=slug)
return render(request, 'myblog/post_detail.html', {'post': post})
在帖子中添加评论
def add_comment_post(request, slug):
form = CommentForm(request.POST or None)
if request.method == "POST":
post = get_object_or_404(Post, slug=slug)
form = CommentForm(request.POST or None)
if form.is_valid():
lst_com = form.save(commit=False)
lst_com.post = post
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
lst_com.ip_address = ip
lst_com.published_date = timezone.now()
lst_com.save()
return render_to_response('myblog/post_comment.html', {'form': form }, RequestContext(request))
ajax request_to_add_comment_post
$('#add_comment').click(function() {
$("input[name=csrfmiddlewaretoken]").val()},
$.ajax({
type: "POST",
url: "comment/",
data: {'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()},
success: addSucsess,
dataType: 'html'
});
});
function addSucsess(data,textStatus,jqXHR)
$('#comment_form').html(data);
现在,我的问题是add_comment_post返回&#34; render_to_response&#34;,再次调用post_detail.view。怎么解决这个?什么是正确的解决方案?