def entry_detail(request, entry_id): entry = get_object_or_404(Entry, id=int(entry_id)) comments = entry.comments(manager='approved').all() comments = nested_comments(comments) comment_form = CommentForm() return render(request, 'entry_detail.html', {'entry': entry, 'comments': comments, 'form': comment_form})
def comments(request): if request.method == 'POST': comment_form = CommentForm(request.POST) entry_id = request.POST.get('comment_entry_id', '') entry = Entry.objects.get(id=int(entry_id)) if comment_form.is_valid(): #print comment_form.cleaned_data author = comment_form.cleaned_data['comment_author'] email = comment_form.cleaned_data['comment_email'] content = comment_form.cleaned_data['comment_content'] url = comment_form.cleaned_data['comment_author_url'] # 如果IP是通过redirect来的,那么就不能直接用'REMOTE_ADDR',要重写 ip = get_client_ip(requst) parent = request.POST.get('comment_parent', '') #print request.POST if not entry_id or not parent: return redirect('/') if author=='' or email=='' or content=='': comments = entry.comments(manager='approved').all() comments = nested_comments(comments) return render(request, 'entry_detail.html', {'entry': entry, 'comments': comments, 'form': comment_form}) entry.comment_count += 1 c = Comments() c.comment_author, c.comment_email = author, email c.comment_content, c.comment_entry = content, entry c.comment_author_url, c.comment_ip = url, ip if parent: c.comment_parent = parent c.save() return redirect('/weblog/article/%s' % entry_id) else: comments = entry.comments(manager='approved').all() comments = nested_comments(comments) return render(request, 'entry_detail.html', {'entry': entry, 'comments': comments, 'form': comment_form}) else: return redirect('/')