Abjilla22
Abjilla22

Reputation: 11

Using a registration form to add to custom user fields (django)

I'm creating custom user models for a registration form in django, but I'm having some problems getting the data into the database. Currently the users can register and their username, password, email, first name and last name are saved, but all the other form data isn't saving into the object. Below are my forms and the output:

Models.py

class Profile(models.Model):
    user = models.OneToOneField(User)
    gender = models.CharField(max_length=20)
    medication = models.CharField(max_length=50, blank=True)
    medical_history = models.CharField(max_length=50,blank=True)
    DOB = models.CharField(max_length=20)
    telephone = models.CharField(max_length=20)
    address = models.CharField(max_length=30)
    city = models.CharField(max_length=20)
    state = models.CharField(max_length=20)
    postcode = models.CharField(max_length=30)

forms.py:

class CreateAccountForm(ModelForm):
    class Meta:
        model = Profile
        fields = ("username","password","password2","first_name","last_name",
                  "gender","medication","medical_history","DOB","email","telephone",
                  "address","city","state","postcode")

views.py:

def create_account(request):
    form = CreateAccountForm(request.POST)
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        password2 = form.cleaned_data['password2']
        first_name = form.cleaned_data['first_name']
        last_name = form.cleaned_data['last_name']
        gender = form.cleaned_data['gender']
        medication = form.cleaned_data['medication']
        medical_history = form.cleaned_data['medical_history']
        DOB = form.cleaned_data['DOB']
        email = form.cleaned_data['email']
        telephone = form.cleaned_data['telephone']
        address = form.cleaned_data['address']
        city = form.cleaned_data['city']
        state = form.cleaned_data['state']
        postcode = form.cleaned_data['postcode']
        if password == password2:
            if (password_verification(password)) == 3:
                if (username_verification(username)) == False:
                    form.save()
                    return HttpResponseRedirect('/login')
                else:
                    return HttpResponseRedirect('/create_account')
            else:
                return HttpResponseRedirect('/create_account')
        else:
            return HttpResponseRedirect('/create_account')
    return render(request, "create_account.html", {'form': form})

In the django admin window, the user is registering to the database with the create_user fields, but none of the added custom fields are saving in the available columns. Any help would be great this is really bugging me. Below is a pic of the empty fields, cheers!

Upvotes: 3

Views: 2321

Answers (1)

Raja Simon
Raja Simon

Reputation: 10305

You are saving User only user.save() so other Profile ( please change your user table to something meaningful Profile.

And also you don't need password first_name last_name email in your profile as well.

And I'll suggest to use the ModelForm to save the Profile table.

user_form = CreateAccountForm(request.POST)
profile_form = ProfileForm(request.POST)    

if user_form.is_valid() and profile_form.is_valid():  
    # save the form after your password check
    user_form.save()
    profile_form.save()

Upvotes: 3

Related Questions