'str'对象不可调用

时间:2014-03-06 10:09:44

标签: python django

我在django中遇到了问题:

'str' object is not callable

代码:

urls.py App Usuarios:

from django.conf.urls import patterns, include, url
from django.contrib.auth.views import login, logout_then_login
from usuarios.views import Registro

urlpatterns = patterns('',
    url(r'^login/$', 'django.contrib.auth.views.login', {'template_name':'registration/login.html'},
                name='login'),

        url(r'^cerrar/$', 'django.contrib.auth.views.logout_then_login', 
        name='logout'),

    url(r'^registro/$', 'Registro', name='registro'),
)

views.py

from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import authenticate, login, logout
from django.core.context_processors import csrf

#importar el formulario de registro
from usuarios.forms import RegistroUsuario


def Registro(request):
    if request.user.is_anonymous():
        if request.method == 'POST':
        form = RegistroUsuario(request, POST)
        if form.is_valid():
                    form.save()
            return HttpResponse('Usuario creado sin problemas.')
            else:
        form = RegistroUsuario()
        context = {}
        context.update(csrf(request))
        context['form'] = form
        #pasar el context al template
        return render_to_response('registro.html', context)
    else:
        return HttpResponseRedirect('/')

forms.py:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm


class RegistroUsuario(UserCreationForm):
    class Meta:
        fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2')

我对django没有多少工作,事实是错误并没有真正告诉我可能出现的问题。

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/usuarios/registro/

Django Version: 1.6.2
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'usuarios')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

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

1 个答案:

答案 0 :(得分:1)

您在网址调度中错误地命名了您的视图:

from usuarios.views import Registro

urlpatterns = patterns('',
    # ...

    url(r'^registro/$', 'Registro', name='registro'),

这里'Registro'不是Django可以导入的名称(它不是完整的模块路径),也没有指定要导入的前缀(patterns()的第一个参数是空字符串{ {1}})。

您应该提供实际的视图对象(您已在此处导入),或者为其提供完整模块路径的名称。

所以选择以下之一:

''

from usuarios.views import Registro

urlpatterns = patterns('',
    # ...

    url(r'^registro/$', Registro, name='registro'),

第一个选项将实际的视图函数传递给寄存器(因为你已经导入了它,所以它可以工作)。

除视图名称外,第二个选项提供模块的完整路径; Django现在可以导入它(它在字符串中查找urlpatterns = patterns('', # ... url(r'^registro/$', 'usuarios.views.Registro', name='registro'), 以确定是否可以导入某些内容。)