我创建了一个表单,用户在其中输入数据并将数据保存到数据库中。但表单不是 s![在此处输入图像描述][1]保存数据编码是否有任何错误?请帮助。 .
我的代码在这里:-
1) views.py
-----------------------------------------------------------
from django.http import HttpResponse
#from django.shortcuts import render_to_response
from django.template import RequestContext, loader
from forms import SignupForm
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
form.cleaned_data('username')
form.cleaned_data('email')
form.cleaned_data('password1')
form.save()
return HttpResponse('/login/') #havent did the login method yet...
else:
t = loader.get_template('signup.html')
cform = SignupForm()
c = RequestContext (request, {'form' : cform,})
return HttpResponse(t.render(c))
#def login(request):
# newform = SignupForm()
#if newform.is_valid():
2)forms.py-
from django.contrib.auth.models import User
from django import forms
class SignupForm(forms.Form):
username = forms.CharField(max_length=255, label="Username")
email = forms.CharField(max_length=255, label="E-Mail Address")
password1 = forms.CharField(max_length=255, label="Password", widget=forms.PasswordInput)
password2 = forms.CharField(max_length=255, label="Repeat Password", widget=forms.PasswordInput)
def clean_username(self):
try:
User.objects.get(username=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError("This username is already in use.Please choose another.")
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError("You must type the same password each time")
return self.cleaned_data
def save(self):
new_user = User.objects.create_user(username=self.cleaned_data['username'], email=self.cleaned_data['email'], password=self.cleaned_data['password1'])
return new_user
3)urls.py:-
from django.conf.urls import patterns, include, url
from django.contrib import admin
#from salary import settings
#from salarylive.views import signup
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'salary.views.home', name='home'),
#url(r'^salary/', include('salary.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^signup/$', 'salarylive.views.signup'),
#url(r'^login/$', 'salarylive.views.login'),
)
错误回溯:-
error traceback :- Environment:
Request Method: POST
Request URL: http://localhost:8000/signup/
Django Version: 1.4.3
Python Version: 2.7.0
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'salarylive')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "c:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "e:\Software Installations\Aptana Workspace\salary\salarylive\views.py" in signup
10. form.cleaned_data('username')
Exception Type: TypeError at /signup/
Exception Value: 'dict' object is not callable