0
我在Django一個問題:'STR' 對象不可調用
'str' object is not callable
代碼:
urls.py應用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
向我們展示您正在獲取的錯誤,以便我們知道它來自哪裏。 – gravetii
對於Python,正確格式化代碼也很重要。您在此網頁上呈現的代碼沒有正確縮進;你的代碼中可能會有空格和製表符的組合(這是很麻煩)。 –
好的。謝謝。對不起,但我第一次在stackoverflow。 – jandresplp