1

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

表格.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)'))

视图.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})

因此,当我们转到 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

追溯

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

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

4

2 回答 2

6

它应该是:

def register(request):
    register_form = RegistrationForm()
    if request.method=='POST':
        form = RegistrationForm(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':register_form})

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

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

于 2013-07-09T06:09:17.980 回答
2

代替

form = regsiter_form(request.POST)

regsiter_form = RegistrationForm(request.POST)

并使用register_formobject 而不是form.

此外,使用数据 fromform.cleaned_data创建用户对象而不是 fromrequest.POST

作为

new_user = User.objects.create_user(username=form.cleaned_data['username'] ...)
于 2013-07-09T06:10:56.103 回答