Skip to content

Instantly share code, notes, and snippets.

@brianarn
Forked from jcroft/django_settings.py
Created January 31, 2012 17:43
Show Gist options
  • Save brianarn/1711795 to your computer and use it in GitHub Desktop.
Save brianarn/1711795 to your computer and use it in GitHub Desktop.
Preventing comment spam
COMMENTS_SECRET = 'any_old_string_you_like'
<input type="hidden" name="comment-secret" id="id_comment-secret" />
<textarea id="id_comment-body" rows="10" cols="40" name="comment-body"></textarea>
<script type="text/javascript">
(function($){
// The elements we'll be working with
var $secretinput = $('#id_comment-secret'),
$commentbody = $('#id_comment-body');
// Create a function to act as the event handler,
// which we can reference by its own name internally
function setSecret(){
// Kick off the timeout
setTimeout(function(){
$secretinput.val('{% setting 'COMMENTS_SECRET' %}');
},
5000);
// Unbind the input so that it's not happening repeatedly
$commentbody.unbind('keypress', setSecret);
}
// Set up the keypress event
// in this case, using bind/unbind. If using jQuery 1.7,
// use on/off instead.
$commentbody.bind('keypress', setSecret);
})(jQuery);
</script>
def clean(self):
cleaned = self.cleaned_data
if not self.request.POST.get('comment-secret', '') == settings.COMMENTS_SECRET:
raise forms.ValidationError("This site thinks you're a spammer. If you're not, wait a few seconds after typing your comment before you submit it.")
return self.cleaned_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment