问题
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?
回答1:
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}),
回答2:
You need to pass the actual form class for "authentication_form" not the name "UsersForm".
来源:https://stackoverflow.com/questions/19469883/django-generic-login-view-return-str-object-not-callable-error