0

I am trying to create custom fields for users to enter on signup with django-allauth. I have referred to several posts about this, but I am not able to get my custom form to save to my database. I do get a combined form on my signup.html page with username, password1 and 2, email and my extra fields of city and school, but I am not able to save the extra fields to the database. I have run syncdb and can see my User Profile table in the admin area.

This advice is the closest I have come to the answer but I do not understand how to implement it: "You can't use UserProfileForm to the allauth.SIGNUP_FORM_CLASS. You need to extend it from SignUpForm and write a save method which will accept the newly created user as the only parameter," from this post: Custom registration form for use with django-allauth

I have also attempted to integrate advice on this form these posts: Django Allauth not saving custom form How to customize user profile when using django-allauth

This is my code: Models.py

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
# A required line - links a UserProfile to User.
user = models.OneToOneField(User)

# The additional attributes we wish to include.
school = models.CharField(max_length=128)
city = models.CharField(max_length=128)

def __unicode__(self):
    return self.user.username

Forms.py

 from django import forms
 from django.contrib.auth.models import User
 from myapp.models import UserProfile
 from django.forms.widgets import HiddenInput
class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('city', 'school')

def signup(self, request, user):
 user=User.objects.get(email=request.email)
 city=request.POST.get('city','')
 school=request.POST.get('school','')
 userprofile_obj = UserProfile(user=user,city=city,school=school)
 userprofile_obj.save()

Settings.py

 ACCOUNT_SIGNUP_FORM_CLASS = 'myapp.forms.UserProfileForm'

My template is the basic Signup.html from the django-allauth templates and I do not have a view made for this, although I attempted to make one from the tangowithdjango user authentication section register view, and this gave similar behavior (not saving to the database).

Thanks, Kelly

Community
  • 1
  • 1

1 Answers1

0

Not sure if this is still an active question/issue for the original poster: if so, and for anyone else who comes across this, a few things to correct to at least move in the right direction:

  • I don't see an __init__() method that calls the superclass? E.g.:

    def __init__(self, *args, **kwargs): super(SignupForm, self).__init__(*args, **kwargs)

  • use the user parameter to the signup method. It should be populated; don't reload it.
  • Ensure the two objects are linking correctly (I didn't use Django to build my profile table so YMMV but I set user.profile = Profile(...); then execute user.profile.save() at the end of my signup() method.
  • get the values to place into the profile from the form cleaned_data (e.g. self.cleaned_data['city'] not the POST.

Then start debugging: is your signup() method firing? What does it get? What happens when you execute the profile.save() method?

geewiz
  • 2,206
  • 1
  • 10
  • 16