Skip to content

Instantly share code, notes, and snippets.

@rwmyers
Created March 25, 2012 18:20
Show Gist options
  • Select an option

  • Save rwmyers/2198831 to your computer and use it in GitHub Desktop.

Select an option

Save rwmyers/2198831 to your computer and use it in GitHub Desktop.
using recaptcha-client
<form action="/blog/add-comment/{{ post.slug }}" method="post">{% csrf_token %}
<script type="text/javascript"
src="http://www.google.com/recaptcha/api/challenge?k=<insert_public_key>">
</script>
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=<insert_public_key>"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
<p><label for="nameField">Name: </label> <input type="text" name="username_field" id="nameField" size="40" maxlength="50"/></p>
<p><label for="locationField">URL: </label> <input type="text" name="user_url_field" id="locationField" size="40" maxlength="120"/></p>
<p>
<label for="bodyField">Comment: </label> <br />
<textarea name="body_field" id="bodyField" rows="8" cols="70"></textarea>
</p>
<input type="submit" value="Submit" />
</form>
pip install recaptcha-client
RECAPTCHA_PRIVATE_KEY = '<private_key_provided_by_reCAPTCHA>'
url(r'^add-comment/(?P<slug>[^\.]+)', 'blog.views.add_comment'),
from recaptcha.client import captcha
from django.shortcuts import redirect, get_object_or_404
from django.http import HttpResponseBadRequest
from django.conf import settings
from blog.models import Post, Comment
def add_comment(request, slug):
if request.method == 'POST':
response = captcha.submit(
request.POST['recaptcha_challenge_field'],
request.POST['recaptcha_response_field'],
settings.RECAPTCHA_PRIVATE_KEY,
request.META["REMOTE_ADDR"])
if response.is_valid:
post = get_object_or_404(Post, slug=slug)
post.comment_set.create(
username = request.POST['username_field'],
user_url = request.POST['user_url_field'],
body = request.POST['body_field']
)
return redirect('view-post', slug=slug)
return redirect('view-post', slug=slug)
else:
return HttpResponseBadRequest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment