django-registration 和 FormWizard 结合使用时出现的问题

发布于 2024-11-16 09:51:11 字数 4780 浏览 3 评论 0原文

我做错了什么,但我不知道是什么。我尝试将 django-registration 0.7 和表单向导结合起来,但出现如下错误:

AttributeError at /
    'BoundField' object has no attribute 'strip
/home/celtrun/rails/neuroweb/site-packages/django/core/handlers/base.py in get_response
                        response = callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/home/celtrun/rails/neuroweb/site-packages/django/utils/decorators.py in _wrapper
            return bound_func(*args, **kwargs) ...
▶ Local vars
/home/celtrun/rails/neuroweb/site-packages/django/utils/decorators.py in _wrapped_view
                    response = view_func(request, *args, **kwargs) ...
▶ Local vars
/home/celtrun/rails/neuroweb/site-packages/django/utils/decorators.py in bound_func
                return func(self, *args2, **kwargs2) ...
▶ Local vars
/home/celtrun/rails/neuroweb/site-packages/django/contrib/formtools/wizard.py in __call__
                return self.done(request, previous_form_list + [form]) ...
▶ Local vars
/home/celtrun/rails/neuroweb/apps/registration/views.py in done
              extra_context=None, formed=form_list[0]) ...
▶ Local vars
/home/celtrun/rails/neuroweb/apps/registration/views.py in register
                             send_email=True, profile_callback=None) ...
▶ Local vars
/home/celtrun/rails/neuroweb/apps/registration/models.py in create_inactive_user
        new_user = User.objects.create_user(username, email, password) ...
▶ Local vars
/home/celtrun/rails/neuroweb/site-packages/django/contrib/auth/models.py in create_user
            email_name, domain_part = email.strip().split('@', 1) '

作为我的 FormWizard,我有 (r'^$', RegistrationWizard([RegistrationForm, CaptchaForm])),:

class RegistrationWizard(FormWizard,):
    def done(self, request, form_list):
        formed = form_list[0]
        register(request, success_url=None,
             form_class=RegistrationForm, profile_callback=None,
             template_name='base.html',
              extra_context=None, formed=form_list[0])
        return redirect('/accounts/register/complete/')
    def get_template(self, steps):
        return ['base.html', 'base.html']

class CaptchaForm(forms.Form):
    recaptcha = ReCaptchaField()       

Register function :

def register(request, success_url=None,
             form_class=RegistrationForm, profile_callback=None,
             template_name='registration/registration_form.html',
             extra_context=None, formed=None):
    if request.method == 'POST':
        if formed: form = formed
        if form.is_valid():
            username = formed['username'] 
            email = formed['email']
            password = formed['password1'] 
            RegistrationProfile.objects.create_inactive_user(username, password, email,
                             send_email=True, profile_callback=None)           
            return HttpResponseRedirect(success_url or reverse('registration_complete'))
    else:
        form = form_class()

    if extra_context is None:
        extra_context = {}
    context = RequestContext(request)
    for key, value in extra_context.items():
        context[key] = callable(value) and value() or value
    return render_to_response(template_name,
                              { 'form': form },
                              context_instance=context)

还有 RegistrationProfile 的 create_inactive_user 函数:

   def create_inactive_user(self, username, password, email,
                             send_email=True, profile_callback=None):
        new_user = User.objects.create_user(username, email, password)
        new_user.is_active = False
        new_user.save()

        registration_profile = self.create_profile(new_user)

        if profile_callback is not None:
            profile_callback(user=new_user)

        if send_email:
            from django.core.mail import send_mail
            current_site = Site.objects.get_current()

            subject = render_to_string('registration/activation_email_subject.txt',
                                       { 'site': current_site })
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())

            message = render_to_string('registration/activation_email.txt',
                                       { 'activation_key': registration_profile.activation_key,
                                         'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
                                         'site': current_site })

            send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [new_user.email])
        return new_user

我希望我没有错过任何重要的内容。有人知道这样的错误可能意味着什么或以其他方式提供帮助吗?

I do something wrong, but I don't know what. I try to combine django-registration 0.7 and form wizard but i getting error like this:

AttributeError at /
    'BoundField' object has no attribute 'strip
/home/celtrun/rails/neuroweb/site-packages/django/core/handlers/base.py in get_response
                        response = callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/home/celtrun/rails/neuroweb/site-packages/django/utils/decorators.py in _wrapper
            return bound_func(*args, **kwargs) ...
▶ Local vars
/home/celtrun/rails/neuroweb/site-packages/django/utils/decorators.py in _wrapped_view
                    response = view_func(request, *args, **kwargs) ...
▶ Local vars
/home/celtrun/rails/neuroweb/site-packages/django/utils/decorators.py in bound_func
                return func(self, *args2, **kwargs2) ...
▶ Local vars
/home/celtrun/rails/neuroweb/site-packages/django/contrib/formtools/wizard.py in __call__
                return self.done(request, previous_form_list + [form]) ...
▶ Local vars
/home/celtrun/rails/neuroweb/apps/registration/views.py in done
              extra_context=None, formed=form_list[0]) ...
▶ Local vars
/home/celtrun/rails/neuroweb/apps/registration/views.py in register
                             send_email=True, profile_callback=None) ...
▶ Local vars
/home/celtrun/rails/neuroweb/apps/registration/models.py in create_inactive_user
        new_user = User.objects.create_user(username, email, password) ...
▶ Local vars
/home/celtrun/rails/neuroweb/site-packages/django/contrib/auth/models.py in create_user
            email_name, domain_part = email.strip().split('@', 1) '

As my FormWizard i have (r'^$', RegistrationWizard([RegistrationForm, CaptchaForm])),:

class RegistrationWizard(FormWizard,):
    def done(self, request, form_list):
        formed = form_list[0]
        register(request, success_url=None,
             form_class=RegistrationForm, profile_callback=None,
             template_name='base.html',
              extra_context=None, formed=form_list[0])
        return redirect('/accounts/register/complete/')
    def get_template(self, steps):
        return ['base.html', 'base.html']

class CaptchaForm(forms.Form):
    recaptcha = ReCaptchaField()       

Register function:

def register(request, success_url=None,
             form_class=RegistrationForm, profile_callback=None,
             template_name='registration/registration_form.html',
             extra_context=None, formed=None):
    if request.method == 'POST':
        if formed: form = formed
        if form.is_valid():
            username = formed['username'] 
            email = formed['email']
            password = formed['password1'] 
            RegistrationProfile.objects.create_inactive_user(username, password, email,
                             send_email=True, profile_callback=None)           
            return HttpResponseRedirect(success_url or reverse('registration_complete'))
    else:
        form = form_class()

    if extra_context is None:
        extra_context = {}
    context = RequestContext(request)
    for key, value in extra_context.items():
        context[key] = callable(value) and value() or value
    return render_to_response(template_name,
                              { 'form': form },
                              context_instance=context)

And create_inactive_user function of RegistrationProfile:

   def create_inactive_user(self, username, password, email,
                             send_email=True, profile_callback=None):
        new_user = User.objects.create_user(username, email, password)
        new_user.is_active = False
        new_user.save()

        registration_profile = self.create_profile(new_user)

        if profile_callback is not None:
            profile_callback(user=new_user)

        if send_email:
            from django.core.mail import send_mail
            current_site = Site.objects.get_current()

            subject = render_to_string('registration/activation_email_subject.txt',
                                       { 'site': current_site })
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())

            message = render_to_string('registration/activation_email.txt',
                                       { 'activation_key': registration_profile.activation_key,
                                         'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
                                         'site': current_site })

            send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [new_user.email])
        return new_user

I hope i did't miss enything important to show. Thas someone know what error like this could means or help any other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱皱眉﹌ 2024-11-23 09:51:11

在您的注册功能中:

email = formed['email']

电子邮件将指向电子邮件表单字段< /em> 不是它的价值。要获取字段的值, 始终使用cleaned_data表单的字典

用户名密码也是如此)

您的代码应该是(在注册中):

...
if request.method == 'POST':
   if formed: form = formed
   if form.is_valid():
      username = form.cleaned_data['username'] 
      email = form.cleaned_data['email']
      password = form.cleaned_data['password1'] 
      RegistrationProfile.objects.create_inactive_user(username, password, email,
                         send_email=True, profile_callback=None)
...

In your registerfunction you have:

email = formed['email']

email will point to the email form field not its value. To get the values of you fields, always use the cleaned_data dictionary of the form.

(Same thing for username and password)

Your code should be (in register):

...
if request.method == 'POST':
   if formed: form = formed
   if form.is_valid():
      username = form.cleaned_data['username'] 
      email = form.cleaned_data['email']
      password = form.cleaned_data['password1'] 
      RegistrationProfile.objects.create_inactive_user(username, password, email,
                         send_email=True, profile_callback=None)
...
莫多说 2024-11-23 09:51:11

我的下意识直觉是你忘记在设置文件中添加中间件行。但老实说我实际上并不知道 - 只是〜本能。

My knee-jerk instinct is that you've forgotten to add a middleware line in your settings file.. But I honestly don't actually know - just ~instinct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文