Laxmi Kadariya
Laxmi Kadariya

Reputation: 1103

Data from the form is not inserted in to user table

I am trying to create signup form adding email field in django from base class UserCreationForm. The code goes as

form.py

class ussignup(UserCreationForm):

  email=forms.EmailField()

  class Meta:
    model=User
    fields=('username','email','password1','password2')

  def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.email=(self.cleaned_data["email"])
        if commit:
            user.save()
        return user

view.py

def signup(request):
    if request.method=='POST':
        form=ussignup(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/rgsuc')

    args={}
    args.update(csrf(request))
    args['form']=ussignup()
    return render_to_response('register.html',args)

urls.py

 url(r'^accounts/signup',signup),

Error output shows the email field . however it is neither validating the required field nor the email field data is inserted to the user database table I tried through this video tutorial here

Upvotes: 1

Views: 62

Answers (1)

Rohan
Rohan

Reputation: 53316

In your view, you are doing

form=signup(request.POST)

instead it should be

form=ussignup(request.POST) 

i.e use form class name to instantiate the form.

Also, its better to show form errors if any when its not valid, you can do that by updating your code as

def signup(request):
    if request.method=='POST':
        form=ussignup(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/rgsuc')
    else:
        form=ussignup()
    args={}
    args.update(csrf(request))
    args['form']=form
    return render_to_response('register.html',args)

Upvotes: 3

Related Questions