Skip to content

Instantly share code, notes, and snippets.

@jcroft
Created January 31, 2012 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jcroft/1711625 to your computer and use it in GitHub Desktop.
Save jcroft/1711625 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">
$('textarea#id_comment-body').keypress(function() {
setTimeout(function(){
$('input#id_comment-secret').attr('value', '{% setting 'COMMENTS_SECRET' %}')
},
5000)
});
</script>
def clean(self):
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