3

Django 登録に ReCaptcha を統合するためのMaro Fucciの投稿をフォローしています。Django 1.4 では正常に動作しました。ただし、Django 1.5 にアップグレードした後、クラス ベースのビューで問題が発生しましたが、これを解決して、ページに登録フォームを表示できるようになりました。最大の問題は、recaptcha がレンダリングされないことです。

私が書いた正確なコードを貼り付けています。

マイ プロジェクト 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')),)

私のアプリケーション 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')),
  )

Marco Fucci ページに投稿されたwidgets.pyandを myapp フォルダー内に配置すると、次のようになりますfields.pymyapp.forms.py

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

manage.py以下に示すように、recaptcha付きの登録フォームを印刷して、登録フォームを印刷しました

>>> 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>

しかし、http://localhost:8000/accounts/register/ブラウザから呼び出すと、recaptcha がレンダリングされません。登録フォームは Recaptcha を継承していません。私は愚かなことをしているのかもしれません。誰かが私を案内してくれませんか。

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

登録ビューを呼び出しているときに RecaptchaRegistrationForm が取得されていることを (デバッグによって) 確認できますか。

また、レンダリングされた html の公開鍵が間違っているようです。6LcPF-KEY

于 2013-06-25T10:36:18.170 に答える