Django Registration' str'对象不可调用

时间:2014-09-03 01:26:13

标签: python django django-urls django-registration django-settings

我在Django项目中使用Django-registration-email。在文档(Django-Registration-Email)中,我被指示在 settings.py 中添加 REGISTRATION_EMAIL_REGISTER_SUCCESS_URL 。但是,这会导致类型错误:

'str' object is not callable

在settings.py中,我将重定向网址设置为:

REGISTRATION_EMAIL_REGISTER_SUCCESS_URL = '/accounts/register/complete/'

并且URL被复制为:

url(
    r'^accounts/register/$',
    RegistrationView.as_view(
        template_name='registration/registration_form.html',
        form_class=CustomEmailRegistrationForm,
        get_success_url=getattr(
            settings,'REGISTRATION_EMAIL_REGISTER_SUCCESS_URL',
            lambda request, user:'/'),
    ),
    name='registration_register',
),

调试信息告诉我第一个错误来自form_valid 中的 /local/lib/python2.7/site-packages/registration/views.py

指示的错误行是

success_url = self.get_success_url(request, new_user)

整个区块是

def form_valid(self, request, form):
    new_user = self.register(request, **form.cleaned_data)
    success_url = self.get_success_url(request, new_user)

    # success_url may be a simple string, or a tuple providing the
    # full argument set for redirect(). Attempting to unpack it
    # tells us which one it is.
    try:
        to, args, kwargs = success_url
        return redirect(to, *args, **kwargs)
    except ValueError:
        return redirect(success_url)

追溯是:

Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/registration/views.py" in dispatch
  79.         return super(RegistrationView, self).dispatch(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/registration/views.py" in post
  35.             return self.form_valid(request, form)
File "/Library/Python/2.7/site-packages/registration/views.py" in form_valid
  83.         success_url = self.get_success_url(request, new_user)

Exception Type: TypeError at /accounts/register/
Exception Value: 'str' object is not callable

有人可以帮我解决这个问题吗?非常感谢!我被这个问题困住了一整天!

2 个答案:

答案 0 :(得分:0)

好的,当你使用

get_success_url=getattr(
        settings,'REGISTRATION_EMAIL_REGISTER_SUCCESS_URL',
        lambda request, user:'/'),
)

在您的网址处理程序中,您将get_success_url设置为字符串。然后,您在form_valid中将其作为函数调用,尝试传递变量。

答案 1 :(得分:0)

最后,我发现问题出在哪里: REGISTRATION_EMAIL_REGISTER_SUCCESS_URL期待的是函数而不是字符串

所以,我应该在settings.py

中添加一行功能
REGISTRATION_EMAIL_REGISTER_SUCCESS_URL = lambda request, user: '/activate/complete/'

无论如何,我仍然愿意转向allauth,因为不再维护django-registration-email。