Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Created March 5, 2010 20:13
Show Gist options
  • Select an option

  • Save nathanborror/323092 to your computer and use it in GitHub Desktop.

Select an option

Save nathanborror/323092 to your computer and use it in GitHub Desktop.
@login_required
def comment_edit(request, comment_id, template_name='comments/edit.html'):
comment = get_object_or_404(Comment, pk=comment_id, user=request.user)
if request.POST:
data = request.POST.copy()
if not data.get('name', ''):
data["name"] = request.user.get_full_name() or request.user.username
if not data.get('email', ''):
data["email"] = request.user.email
form = CommentForm(comment.content_object, data)
if form.is_valid():
comment.comment = form.cleaned_data['comment']
comment.save()
request.user.message_set.create(message="Your comment has been edited.")
return HttpResponseRedirect(comment.content_object.get_absolute_url())
else:
form = CommentForm(comment.content_object, initial={'comment': comment.comment})
return render(request, template_name, {
'form': form,
'comment': comment.comment
})
@login_required
def comment_delete(request, comment_id, template_confirm_name='comments/confirm_delete.html'):
comment = get_object_or_404(Comment, pk=comment_id, user=request.user)
if request.POST:
content_object = comment.content_object
comment.delete()
request.user.message_set.create(message="Your comment has been deleted.")
return HttpResponseRedirect(content_object.get_absolute_url())
else:
return render(request, template_confirm_name, {'comment': comment})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment