Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Enable comment posting/error via ajax

  • Loading branch information...
commit d65db0d9f181ca535b51e78ab3309cd22ff1e568 1 parent 2784abe
Patrick Altman paltman authored

Showing 1 changed file with 25 additions and 2 deletions. Show diff stats Hide diff stats

  1. +25 2 dialogos/views.py
27 dialogos/views.py
... ... @@ -1,3 +1,7 @@
  1 +import json
  2 +
  3 +from django.http import HttpResponse
  4 +
1 5 from django.views.decorators.http import require_POST
2 6 from django.shortcuts import get_object_or_404, redirect
3 7
@@ -10,13 +14,32 @@
10 14
11 15
12 16 @require_POST
13   -def post_comment(request, content_type_id, object_id):
  17 +def post_comment(request, content_type_id, object_id, form_class=CommentForm):
14 18 content_type = get_object_or_404(ContentType, pk=content_type_id)
15 19 obj = get_object_or_404(content_type.model_class(), pk=object_id)
16   - form = CommentForm(request.POST, request=request, obj=obj, user=request.user)
  20 + form = form_class(request.POST, request=request, obj=obj, user=request.user)
17 21 if form.is_valid():
18 22 comment = form.save()
19 23 commented.send(sender=post_comment, comment=comment, request=request)
  24 + if request.is_ajax():
  25 + return HttpResponse(json.dumps({
  26 + "status": "OK",
  27 + "comment": {
  28 + "pk": comment.pk,
  29 + "comment": comment.comment,
  30 + "author": comment.author.username,
  31 + "name": comment.name,
  32 + "email": comment.email,
  33 + "website": comment.website,
  34 + "submit_date": str(comment.submit_date)
  35 + }
  36 + }), mimetype="application/json")
  37 + else:
  38 + if request.is_ajax():
  39 + return HttpResponse(json.dumps({
  40 + "status": "ERROR",
  41 + "errors": form.errors
  42 + }), mimetype="application/json")
20 43 redirect_to = request.POST.get("next")
21 44 # light security check -- make sure redirect_to isn't garbage.
22 45 if not redirect_to or " " in redirect_to or redirect_to.startswith("http"):

0 comments on commit d65db0d

Please sign in to comment.
Something went wrong with that request. Please try again.