Question

I am following the post by Maro Fucci for integrating ReCaptcha for my Django Registration. It worked properly in Django 1.4. However, after upgrading to Django 1.5 I got problem with class based views which I have resolved it and now I can able to render the registration form on the page. The biggest problem is the recaptcha its not rendering.

I am pasting the exact code which I have written.

My Projects urls.py

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
          ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns = patterns('',

  url(r'^$', 'myapp.views.index'),
  (r'^accounts/', include('registration.backends.default.urls')),)

My Application urls.py

from django.conf.urls.defaults import *
from registration.views import RegistrationView

from myapp.forms import RecaptchaRegistrationForm

urlpatterns = patterns('',
    url(r'^register/$', RegistrationView.register,
    {'form_class': RecaptchaRegistrationForm},
    name='registration.RegistrationView.register'),
    (r'', include('registration.backends.default.urls')),
  )

I have placed the widgets.py and fields.py which was posted in Marco Fucci page inside the myapp folder and myapp.forms.py looks like this

from django import forms
from myapp.fields import ReCaptchaField 
from registration.forms import RegistrationForm
class RecaptchaRegistrationForm(RegistrationForm):
      recaptcha = ReCaptchaField()

I have printed the registration form using the manage.py it prints registration form with recaptcha as shown below

>>> from myapp.forms import RecaptchaRegistrationForm
>>> p = RecaptchaRegistrationForm()
>>> print p
<tr class="required"><th><label for="id_username">Username:</label></th><td><input id="id_username" maxlength="30" name="username" type="text" /></td></tr>
<tr class="required"><th><label for="id_email">E-mail:</label></th><td><input id="id_email" name="email" type="text" /></td></tr>
<tr class="required"><th><label for="id_password1">Password:</label></th><td><input id="id_password1" name="password1" type="password" /></td></tr>
<tr class="required"><th><label for="id_password2">Password (again):</label></th><td><input id="id_passwo" name="password2" type="password" /></td></tr>
<tr class="required"><th><label for="id_recaptcha">Recaptcha:</label></th><td> <script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=6LcPF-KEY"></script>

But when I call http://localhost:8000/accounts/register/ from browser recaptcha is not rendering. The registration form is not inheriting the Recaptcha. May be I am doing something stupid. Can some one guide me please.

Was it helpful?

Solution

When using a class based view in your url patterns, point your url pattern to the as_view() method. You can't just include the RegistrationView.register method directly.

To customize the behaviour of RegistrationView, subclass it, and set the form_class attribute.

class RecaptchaRegistrationView(RegistrationView):
    """
    Subclass of RegistrationView that uses RecaptchaRegistrationForm
    """
    form_class = RecaptchaRegistrationForm

urlpatterns = patterns('',
    url(r'^register/$', RecaptchaRegistrationView.as_view(), name='registration_register'),

OTHER TIPS

Can you make sure (by debugging) that your RecaptchaRegistrationForm is getting picked up when you are calling the register view.

And also, The public key in the rendered html seems to be wrong. 6LcPF-KEY

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top