在django中创建表单时,object不可调用错误

时间:2013-07-09 05:53:59

标签: django python-2.7 django-forms

我正在使用django并尝试创建注册表单,下面是我的代码

forms.py

from django import forms

attrs_dict = { 'class': 'required' }

class RegistrationForm(forms.Form):
    username = forms.RegexField(regex=r'^\w+$',
                                max_length=30,
                                widget=forms.TextInput(attrs=attrs_dict),
                                label=_(u'username'))
    email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,maxlength=75)),
                                label=_(u'email address'))
    password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
                                label=_(u'password'))
    password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
                                label=_(u'password (again)'))

views.py

from authentication.forms import RegistrationForm

def register(request):
    regsiter_form = RegistrationForm()
    if request.method=='POST':
        form = regsiter_form(request.POST)
        if form.is_valid():
            new_user = User.objects.create_user(username=request.POST['username'], 
                                                email=request.POST['email'], 
                                                password=request.POST['password1'])
            new_user.is_active = False
            new_user.save()
            return HttpResponseRedirect(reverse('index'))
    return render_to_response('registration/registration_form.html'{'form':regsiter_form})

所以当我们转到网址时,会显示一个注册表单,当我们输入详细信息并点击提交时,我收到以下错误

TypeError at /accounts_register/register/
'RegistrationForm' object is not callable
Request Method: POST
Request URL:    http://localhost:8000/accounts_register/register/
Django Version: 1.5.1
Exception Type: TypeError
Exception Value:    
'RegistrationForm' object is not callable

回溯

▶ Local vars
/home/user/package/authentication/views.py in register
        form = regsiter_form(request.POST) 

所以任何人都可以告诉我为什么上面的表单对象抱怨,因为对象不可调用,我们需要进行更改以避免此错误。

2 个答案:

答案 0 :(得分:3)

应该是:

def register(request):
    regsiter_form = RegistrationForm()
    if request.method=='POST':
        form = RegistraionForm(request.POST)
        if form.is_valid():
            new_user = User.objects.create_user(username=request.POST['username'], 
                                            email=request.POST['email'], 
                                            password=request.POST['password1'])
            new_user.is_active = False
            new_user.save()
            return HttpResponseRedirect(reverse('index'))
    return render_to_response('registration/registration_form.html'{'form':regsiter_form})

因此,form = regsiter_form(request.POST)应在POST检查中form = RegistrationForm(request.POST)

重点是您首先使用regsiter_form = RegistrationForm()创建了RegistrationForm的对象/实例,然后您尝试了regsiter_form(request.POST),所以基本上您尝试再次调用不允许的对象/实例,除非那里是您班上定义的__call__方法。

答案 1 :(得分:2)

而不是

form = regsiter_form(request.POST)

DO

regsiter_form = RegistrationForm(request.POST)

使用register_form对象代替form

此外,使用form.cleaned_data中的数据创建用户对象而不是request.POST

作为

new_user = User.objects.create_user(username=form.cleaned_data['username'] ...)