3

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.

4

2 回答 2

2

在您的 url 模式中使用基于类的视图时,请将您的 url 模式指向该as_view()方法。您不能直接包含该RegistrationView.register方法。

要自定义 的行为RegistrationView,对其进行子类化并设置form_class属性。

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

urlpatterns = patterns('',
    url(r'^register/$', RecaptchaRegistrationView.as_view(), name='registration_register'),
于 2013-06-25T10:16:57.630 回答
0

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

于 2013-06-25T10:36:18.170 回答