在Django中创建表单时对象不是可调用错误

Shi*_*dla 1 django django-forms python-2.7

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

表格

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)'))
Run Code Online (Sandbox Code Playgroud)

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})
Run Code Online (Sandbox Code Playgroud)

因此,当我们转到url时,将显示注册表格,当我们输入详细信息并单击“提交”时,出现以下错误

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
Run Code Online (Sandbox Code Playgroud)

追溯

? Local vars
/home/user/package/authentication/views.py in register
        form = regsiter_form(request.POST) 
Run Code Online (Sandbox Code Playgroud)

所以任何人都可以让我知道为什么上面的表单对象会抱怨,因为该对象不可调用,我们需要在哪里进行更改以避免此错误。

Aks*_*aaj 5

它应该是:

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})
Run Code Online (Sandbox Code Playgroud)

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

关键是您首先使用创建了RegistrationForm的对象/实例regsiter_form = RegistrationForm(),然后尝试了regsiter_form(request.POST),因此基本上,您试图再次调用一个对象/实例,除非__call__您的类中定义了一个方法,否则这是不允许的。