Django通用登录视图返回'str object not callable'错误

时间:2013-10-19 19:04:03

标签: python django django-forms django-views django-authentication

我在myProject中的urls.py是

from django.conf.urls import patterns, include, url
from testapp import urls

urlpatterns = patterns('',
    url(r'^', include(urls)),
)

和myApp中的urls.py(称为testapp)是

from django.conf.urls import patterns, include, url
from testapp.forms import UsersForm

urlpatterns = patterns('',
    url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'testapp/templates/login.html', 'authentication_form':'UsersForm'}),
)

我的myProject / templates / login.html是

        <form method="post" action="{% url 'django.contrib/auth.views.login' %}">{% csrf_token%}
            {{form.username.label_tag}}
            {{form.username}}
            {{form.password.label_tag}}
            {{form.password}}
        <input type="submit" value="login" />
        <input type="hidden" name="next" value="{{ next }}" />
        </form>

现在,当我运行server并转到127.0.0.1时,它说

TypeError at /

'str' object is not callable
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.5.2
Exception Type: TypeError
Exception Value:    
'str' object is not callable
Exception Location: /usr/local/lib/python2.7/dist-packages/django/contrib/auth/views.py   in login, line 53
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in  get_response
  115.                         response = callback(request, *callback_args,    **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/debug.py" in   sensitive_post_parameters_wrapper
  69.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/views.py" in login
  53.         form = authentication_form(request)

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

我正在使用django提供的通用登录视图。为什么它在通用登录视图中的第53行给出了错误?在我的urls.py中,我确实指定了'authentication_form':'UsersForm'。我是否错误地导入了UsersForm?

2 个答案:

答案 0 :(得分:2)

您需要传递"authentication_form"的实际表单类,而不是名称"UsersForm"

答案 1 :(得分:2)

清除托马斯的答案,这是对的。
传递表格本身,而不是字符串:

from testapp.forms import UsersForm
url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'testapp/templates/login.html', 'authentication_form':UsersForm}),