0

我在 myProject 中的 urls.py 是

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

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

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

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>

现在,当我运行服务器并转到 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”。我是否错误地导入了用户表单?

4

2 回答 2

2

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

于 2013-10-19T19:09:07.173 回答
2

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

from testapp.forms import UsersForm
url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'testapp/templates/login.html', 'authentication_form':UsersForm}),
于 2013-10-19T19:26:05.617 回答