1

I am using django and trying to create a registration form and below are my codes

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})

so when we go to the url,a registration form is displaying and when we enter the details and clicked submit i am getting the following error

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

Traceback

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

So can anyone please let me know why the above form object is complaining as the object is not callable and where we need to make the changes in order to avoid this error.

2 Answers 2

6

It should be:

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})

So, form = register_form(request.POST) should be form = RegistrationForm(request.POST) inside your POST check.

The point is you first created an object/instance of RegistrationForm using register_form = RegistrationForm(), and then you tried register_form(request.POST), so basically you are trying to again call an object/instance which is not allowed unless there is a __call__ method defined on your class.

Sign up to request clarification or add additional context in comments.

Comments

2

Instead of

form = regsiter_form(request.POST)

do

regsiter_form = RegistrationForm(request.POST)

And use register_form object instead of form.

Also, use data from form.cleaned_data to create user object instead of from request.POST

As

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.