How to set up user profile when using django-allauth

I have a django project with django-allauth application. I need to collect additional data from the user during registration. I came across a similar one here , but unfortunately no one answered the profile settings part.

Per documentation provided for django-allauth :

ACCOUNT_SIGNUP_FORM_CLASS (= None )

A string indicating the user form class (for example, 'myapp.forms.SignupForm' ), which is used during registration to ask the user to enter an additional input (for example, newsletter subscription, date of birth). This class should implement the 'save' method, which takes the new registered user as the only parameter.

I am new to django and struggling with this. Can anyone provide an example of such a custom form class? Do I need to add a model class with a reference to a user object, for example this ?

+88
django profile django-allauth
Sep 06 '12 at 15:30
source share
7 answers

Suppose you want to ask the user about his first / last name during registration. You will need to put these fields in your form, for example:

 class SignupForm(forms.Form): first_name = forms.CharField(max_length=30, label='Voornaam') last_name = forms.CharField(max_length=30, label='Achternaam') def signup(self, request, user): user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.save() 

Then in your settings point to this form:

 ACCOUNT_SIGNUP_FORM_CLASS = 'yourproject.yourapp.forms.SignupForm' 

What all.

+141
Sep 06 '12 at 21:40
source share

Using the solution suggested by pennersr, I get a wear message:

 DeprecationWarning: The custom signup form must offer a def signup(self, request, user) method DeprecationWarning) 

This is due to the fact that since version 0.15 the save method has been deprecated in favor of the registration registration method (request, user).

So, to solve this problem, the example code should look like this:

 class SignupForm(forms.Form): first_name = forms.CharField(max_length=30, label='Voornaam') last_name = forms.CharField(max_length=30, label='Achternaam') def signup(self, request, user): user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.save() 
+22
Jun 15 '14 at 16:13
source share

Here is what worked for me, combining several other answers (none of them are 100% full and DRY).

In yourapp/forms.py :

 from django.contrib.auth import get_user_model from django import forms class SignupForm(forms.ModelForm): class Meta: model = get_user_model() fields = ['first_name', 'last_name'] def signup(self, request, user): user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.save() 

And in settings.py :

 ACCOUNT_SIGNUP_FORM_CLASS = 'yourapp.forms.SignupForm' 

This way he uses the forms of the model to be DRY, and uses the new def signup . I tried to put 'myproject.myapp.forms.SignupForm' , but this somehow led to an error.

+13
Jan 27 '16 at 5:34
source share

In users/forms.py you put:

 from django.contrib.auth import get_user_model class SignupForm(forms.ModelForm): class Meta: model = get_user_model() fields = ['first_name', 'last_name'] def save(self, user): user.save() 

In settings.py you put:

 ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.SignupForm' 

Thus, you do not violate the DRY principle for the definition of user model field models.

+4
Jul 12 '15 at 11:56
source share

@Shreyas: the solution below may not be the cleanest, but it works. Please let me know if you have any suggestions for cleaning it.

To add information that is not related to the default user profile, first create a model in the yourapp / models.py file. Read the general django docs to learn more about this, but mostly:

 from django.db import models class UserProfile(models.Model): user = models.OneToOneField(User, related_name='profile') organisation = models.CharField(organisation, max_length=100, blank=True) 

Then create the form in yourapp / forms.py:

 from django import forms class SignupForm(forms.Form): first_name = forms.CharField(max_length=30, label='Voornaam') last_name = forms.CharField(max_length=30, label='Achternaam') organisation = forms.CharField(max_length=20, label='organisation') def signup(self, request, user): user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] # Replace 'profile' below with the related_name on the OneToOneField linking back to the User model up = user.profile up.organisation = self.cleaned_data['organisation'] user.save() up.save() 
+3
Jul 27 '16 at 15:29
source share

I tried a lot of different tutorials and they all have something missing, repeating unnecessary code or doing strange things, below are my solution, which combines all the parameters that I found, it works, I already entered it in but it's still not convinces me because I expect to get first_name and last_name inside functions that I attached to users to avoid creating a profile inside the form, but I couldn’t, in my opinion, help you.

Models.py

 class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(max_length=500, blank=True) nationality = models.CharField(max_length=2, choices=COUNTRIES) gender = models.CharField(max_length=1, choices=GENDERS) def __str__(self): return self.user.first_name @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) @receiver(post_save, sender=User) def save_user_profile(sender, instance, **kwargs): instance.profile.save() 

Forms.py

 class SignupForm(forms.ModelForm): first_name = forms.CharField(max_length=100) last_name = forms.CharField(max_length=100) class Meta: model = Profile fields = ('first_name', 'last_name', 'nationality', 'gender') def signup(self, request, user): # Save your user user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.save() user.profile.nationality = self.cleaned_data['nationality'] user.profile.gender = self.cleaned_data['gender'] user.profile.save() 

Settings.py

 ACCOUNT_SIGNUP_FORM_CLASS = 'apps.profile.forms.SignupForm' 
+3
Apr 08 '17 at 19:58 on
source share

Create a profile model with a user as OneToOneField

 class Profile(models.Model): user = models.OneToOneField(User, verbose_name=_('user'), related_name='profiles') first_name=models.CharField(_("First Name"), max_length=150) last_name=models.CharField(_("Last Name"), max_length=150) mugshot = ImageField(_('mugshot'), upload_to = upload_to, blank=True) phone= models.CharField(_("Phone Number"), max_length=100) security_question = models.ForeignKey(SecurityQuestion, related_name='security_question') answer=models.CharField(_("Answer"), max_length=200) recovery_number= models.CharField(_("Recovery Mobile Number"), max_length=100) city=models.ForeignKey(City,related_name='city', blank=True, null=True, help_text=_('Select your City')) location=models.ForeignKey(Country,related_name='location', blank=True, null=True, help_text=_('Select your Location')) 
-8
Sep 06 '12 at 15:38
source share



All Articles