简体   繁体   中英

Django Registration 'str' object is not callable

I am using Django-registration-email in my Django project. In there documentation ( Django-Registration-Email ), I am instructed to add REGISTRATION_EMAIL_REGISTER_SUCCESS_URL in the settings.py . However, this is causing the type error:

'str' object is not callable

In the settings.py, I set the redirect url as such:

REGISTRATION_EMAIL_REGISTER_SUCCESS_URL = '/accounts/register/complete/'

And the the url is copied as such:

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',
),

And the debug information told me that the first error comes from /local/lib/python2.7/site-packages/registration/views.py in form_valid

The indicated error line is

success_url = self.get_success_url(request, new_user)

The whole block is

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)

The traceback is:

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

Can anyone help me to solve this issue? Thanks a lot! I am stuck by this problem for one whole day!

OK, when you use

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

in your url handler, you are setting get_success_url as a string. You are then calling it in form_valid , as a function, trying to pass it variables.

Finally, I find where is the problem: REGISTRATION_EMAIL_REGISTER_SUCCESS_URL is expecting a function rather than a string

So, I should add an one-line function in the settings.py

REGISTRATION_EMAIL_REGISTER_SUCCESS_URL = lambda request, user: '/activate/complete/'

Anyway, I still would like to move to allauth since django-registration-email is no longer maintained.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM