jandresplp
jandresplp

Reputation: 25

'str' object not callable

I have a problem in django:

'str' object is not callable

Code:

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

I not worked much with django and the truth is that the error is not really telling me what might be wrong.

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

Upvotes: 0

Views: 947

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121864

You named your view incorrectly in the URL dispatch:

from usuarios.views import Registro

urlpatterns = patterns('',
    # ...

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

Here 'Registro' is not a name Django can import (it is not a full module path), nor did you specify a prefix to import from (the first argument to patterns() is an empty string '').

You should either give the actual view object (that you already imported here), or give it the name for the full module path.

So pick one of:

from usuarios.views import Registro

urlpatterns = patterns('',
    # ...

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

or

urlpatterns = patterns('',
    # ...

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

The first option passes the actual view function to the register (which works because you imported it already).

The second option gives the full path to the module in addition to the view name; Django can now import that (it looks for the . in the string to determine if something can be imported).

Upvotes: 1

Related Questions