I have written this signal receiver in django 1.6 that is intended to block posted comments containing bad words from being saved database:
@receiver(pre_save, sender= Comment)
def spam_filter(sender, **kwargs):
cmnt = kwargs['instance']
my_expression = '|'.join(re.escape(word) for word in BAD_WORDS)
if re.search(my_expression, cmnt.body, flags=re.IGNORECASE):
#pervent the comment from being saved
else:
pass
I am wondering how to tell django in place of '#pervent comment being saved' to not save the 'bad' comment instance?
P.S. the veiw:
@login_required
def add_comment(request, post_id):
p= Blog.objects.get(id=post_id)
post_slug = p.slug
cform = CommentForm(request.POST)
if cform.is_valid():
c = cform.save(commit = False)
c.created = timezone.now()
c.post = p
c.author = request.user
c.save()
args=post_slug
messages.info(request, "comment was added")
return HttpResponseRedirect(reverse("Blog.views.post_withslug",
args=[post_slug]))