Django通用登录视图返回'str object not callable'错误 [英] Django generic login view return 'str object not callable' error

查看:1055
本文介绍了Django通用登录视图返回'str object not callable'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的myProject中的urls.py是

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

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

来自django.conf.urls导入模式的包含来自testapp.forms的url 
import UsersForm

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

我的myProject / templates / login.html是

 < form method =postaction ={%url'django.contrib / auth.views.login'%}> {%csrf_token%} 
{ form.username.label_tag}}
{{form.username}}
{{form.password.label_tag}}
{{form.pas sword}}
< input type =submitvalue =login/>
< input type =hiddenname =nextvalue ={{next}}/>
< / form>

现在,当我运行服务器并转到127.0.0.1它说

  Type $ / 

'str'对象不可调用
请求方法:GET
请求URL:http ://127.0.0.1:8000 /
Django版本:1.5.2
异常类型:TypeError
异常值:
'str'对象不可调用
异常位置:/usr/local/lib/python2.7/dist-packages/django/contrib/auth/views.py登录,第53行
追溯:
文件/ usr / local / lib / python2.7 / dist-packages / django / core / handlers / base.pyin get_response
115. response = callback(request,* callback_args,** callback_kwargs)
文件/ usr / local / lib / python2.7 / dist-packages / django / views / decorators / debug.pyin sensitive_post_parameters_wrapper
69. return view(request,* args,** kwargs)
文件/ usr / local /lib/python2.7/dist-packages/django/utils/decorators.py在_wrapped_view
91。 response = view_func(request,* args,** kwargs)
文件/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py在_wrapped_view_func
89. response = view_func(request,* args,** kwargs)
文件/usr/local/lib/python2.7/dist-packages/django/contrib/auth/views.py在登录
53. form = authentication_form(request)

异常类型:TypeError at /
异常值:'str'对象不可调用
/ pre>

我正在使用django提供的通用登录视图。为什么在通用登录视图中的第53行给我一个错误?在我的urls.py中,我确实指定了'authentication_form':'UsersForm'。我导入用户窗体错误吗?

解决方案

要清除托马斯的答案,那是正确的。

传递表单本身,而不是字符串:

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


My urls.py in myProject is

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

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

and my urls.py in myApp (called testapp) is

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

My myProject/templates/login.html is

        <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>

Now, when I runserver and go to 127.0.0.1 it says

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

I am using the generic login view which django provides. Why is it giving me an error at line 53 in the generic login view? In my urls.py I did specify that 'authentication_form':'UsersForm'. Am I importing the UsersForm incorrectly?

解决方案

To clear out Thomas's answer, which is right.
Pass the form itself, not the string:

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

这篇关于Django通用登录视图返回'str object not callable'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆