1

我在我的Django項目中使用Django註冊電子郵件。在那裏的文件(Django-Registration-Email),我被指示添加REGISTRATION_EMAIL_REGISTER_SUCCESS_URLsettings.py。然而,這是造成錯誤類型:Django註冊'str'對象不可調用

'str' object is not callable 

在settings.py,我設置了重定向URL這樣:

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

調試信息告訴我,第一個錯誤來自/local/lib/python2.7/site-packages/registration/views.py in form_valid

指示的錯誤行是

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 

誰能幫我解決這個問題?非常感謝!我被這個問題困住了一整天!

+0

你可以發佈完整的Traceback嗎? – shaktimaan 2014-09-03 01:27:49

+0

它可能與此相關嗎? http://stackoverflow.com/questions/10809240/typeerror-str-object-is-not-callable-django-python?rq=1 – faboolous 2014-09-03 01:41:35

+0

我已經添加了回溯以及重定向url的設置。 – 2014-09-03 02:48:07

回答

0

OK,當您在URL處理器使用

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

,要設置get_success_url爲字符串。然後,您將其作爲函數form_valid進行調用,嘗試將其傳遞給變量。

+0

嗨GAEfan,謝謝你的回答。其實我從Django註冊電子郵件的文檔(鏈接:https://github.com/bitmazk/django-registration-email)複製這段代碼。我認爲他們正試圖將重定向url傳遞給後端視圖。你知道如何解決它嗎?非常感謝,您的回答非常有幫助! – 2014-09-03 03:27:27

+0

你正在混合2個不同的包:django-registration和django-registration-email,他們的url處理程序是不同的。如果你把'REGISTRATION_EMAIL_REGISTER_SUCCESS_URL ='/ accounts/register/complete /'放在'settings.py'中,那麼'get_success_url'將總是以字符串形式返回。你需要選擇一個包或其他,並且只使用它。 – GAEfan 2014-09-03 03:54:37

+0

Django的註冊電子郵件是基於Django的登記來包裝,所以我需要包括在我的settings.py INSTALLED_APPS,通過文檔的指示。你知道什麼是「GETATTR(設置,‘REGISTRATION_EMAIL_REGISTER_SUCCESS_URL’,拉姆達的要求,用戶:‘/’)」在做什麼?爲什麼使用lambda?我如果你讀Django中註冊的電子郵件的'Important'部分種遺憾使用Django註冊的電子郵件 – 2014-09-03 04:02:59

0

最後,我發現問題出在哪裏:REGISTRATION_EMAIL_REGISTER_SUCCESS_URL期待的功能,而不是字符串

所以,我應該在settings.py添加一個行功能

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

無論如何,我仍然想搬到allauth,因爲django-registration-email不再維護。

相關問題